I'm new to php. Now I am doing one script that works with time. I have an array of time, which I released as 930,1030,1230, etc. With this array, various actions are performed using functions and, as a result, I need to return the time in unix format. According to my idea it looks like this - we start today at unix with the mktime function, and then add 930,1030,1230, etc. But I forgot to take into account that such numbers cannot simply be simply multiplied by the clock (3600), which will be nekkorrectno. The question arises - is it possible to transfer date type 930 to unixtime using php functions?
2 answers
SUBSTR
if I understand correctly, then 930 is 9:30. Then the easiest way
$minutes = intVal(substr($wierdTime,-2)); $hours = intVal(substr($wierdTime,0,-2)); NB Don't write the date like that.
- wow! I just couldn’t for some reason bring the format with: I now understand that it was in vain ... thanks !! I will try your method! - Artyom Grafsky
|
You can add leading zero to the clock and use the createFromFormat method:
$time = '930'; $dt = DateTime::createFromFormat('Hi', str_pad($time, 4, '0', STR_PAD_LEFT)); echo $dt->getTimestamp(), PHP_EOL; // 1530171000 - unix timestamp echo $dt->format('Ymd H:i'); // 2018-06-28 09:30 - тут будет текущий день If you only need the number of seconds from the current midnight (is it unixtime?), You can count it:
echo ($dt->format('H')*60 + $dt->format('i'))*60; //34200 |