I have to convert time in format of hh:mm:ss which is over 24hr, for instance 34:23:00. Is there any simple solution for this without using explode + strtotime functions?
This should give you what you're looking for (assuming 1 second = 1000000 microseconds).
$time = '34:23:00';
$parts = explode(':', $time);
$hours = !empty($parts[0]) ? $parts[0] : 0;
$minutes = !empty($parts[1]) ? $parts[1] : 0;
$seconds = !empty($parts[2]) ? $parts[2] : 0;
$microseconds = (($hours * 60 * 60) + ($minutes * 60) + ($seconds)) * 1000000;
This should do the trick
<?php
$inputTime = "34:23:00";
$timeSplitted = explode(":", $inputTime);
$microSeconds = ($timeSplitted[0]*3600 + $timeSplitted[1]*60 + $timeSplitted[2]*1)*1000;
var_dump($microSeconds); // int(123780000)
Related
I want to convert time (hh:mm:ss) into milliseconds. Then how can I do that?
Ex:
Time is: 00:00:11
Note: I need a code in PHP.
$string = "00:00:11";
$time = explode(":", $string);
$hour = $time[0] * 60 * 60 * 1000;
$minute = $time[1] * 60 * 1000;
$sec = $time[2] * 1000;
$result = $hour + $minute + $sec;
echo $result;
$time = '11:22:33';
$seconds = strtotime("1970-01-01 $time UTC");
$miliseconds = $seconds * 1000;
echo $seconds ."\n" . $miliseconds;
output:
40953
40953000
Date to time conversion will be like as follows
$Given_date = date('H:i:s');
$hour = date('H',strtotime($Given_date));
$minute = date('i',strtotime($Given_date));
$seconds = date('s',strtotime($Given_date));
$sec_to_milli = $seconds * 1000; //seconds to milliseconds
$min_to_milli = $minute * 60 * 1000; //minutes to milliseconds
$hrs_to_milli = $hour * 60 * 60 * 1000; //hours to milliseconds
$milliseconds = $hrs_to_milli + $min_to_milli + $sec_to_milli;
echo $milliseconds;
I am trying to get a total time from few times written as a strings in php.
Times as strings:
00.25, 07.35, 01.10, 00.00, 01.00, 00.22
With that I should get 10hrs12mins but I get 11hrs03mins.
function add()
{
$midnight = strtotime('0.00');
$extra = strtotime(hours($extratime));
$taxi = strtotime(hours($taxitime));
$flt = strtotime($flttime);
$res = strtotime(hours($options->restime));
$hold = strtotime(hours($holdtime));
$totalseconds = $extra + $taxi + $flt + $res + $hold;
return date("H.i", $midnight + $totalseconds);
}
$holdtime, $taxitime, $extratime, $options->restime are all written as minutes and then converted to hours using:
function hours($min)
{
$mins = abs($min);
$neg = ($min < 0) ? '-' : '' ;
$hours = $mins / 60;
$onlyhours = floor($hours);
$onlymins = $mins - ($onlyhours*60);
$time = sprintf("%s%d.%02d",$neg,$onlyhours,$onlymins);
return $time;
}
The function hours works fine but function add doesn't as after displaying it I get 11.03 where it should be 10.12.
Thanks in advance,
Maciej.
Do not convert $holdtime, $taxitime, $extratime, $options->restime in hours.
Add these durations in minutes :
$total = $holdtime + $taxitime + $extratime + $options->restime;
Then convert $total in hours.
Can you please let me know how to convert mm:ss to milliseconds in PHP.
$value = "10:10"
$ms = ...
No need to do string conversions or array manipulations:
sscanf($value, "%d:%d", $minutes, $seconds);
$ms = $seconds * 1000 + $minutes * 60 * 1000;
$ms = intval(substr($value, 0, 2))*60*1000 + intval(substr($value, 2, 2))*1000;
$value = "10:10";
$time = explode(':',$value);
$ms = $time[0]*60000 + $time[1]*1000;
$value = "10:10";
list($minutes, $seconds) = explode(':', $value);
$milliseconds = $seconds * 1000 + $minutes * 60000;
var_dump($milliseconds); // 610000
How to turn time in format HH:MM:SS into a flat seconds number?
P.S. Time could be sometimes in format MM:SS only.
No need to explode anything:
$str_time = "23:12:95";
$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = $hours * 3600 + $minutes * 60 + $seconds;
And if you don't want to use regular expressions:
$str_time = "2:50";
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
I think the easiest method would be to use strtotime() function:
$time = '21:30:10';
$seconds = strtotime("1970-01-01 $time UTC");
echo $seconds;
// same with objects (for php5.3+)
$time = '21:30:10';
$dt = new DateTime("1970-01-01 $time", new DateTimeZone('UTC'));
$seconds = (int)$dt->getTimestamp();
echo $seconds;
demo
Function date_parse() can also be used for parsing date and time:
$time = '21:30:10';
$parsed = date_parse($time);
$seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];
demo
If you will parse format MM:SS with strtotime() or date_parse() it will fail (date_parse() is used in strtotime() and DateTime), because when you input format like xx:yy parser assumes it is HH:MM and not MM:SS. I would suggest checking format, and prepend 00: if you only have MM:SS.
demo strtotime()
demo date_parse()
If you have hours more than 24, then you can use next function (it will work for MM:SS and HH:MM:SS format):
function TimeToSec($time) {
$sec = 0;
foreach (array_reverse(explode(':', $time)) as $k => $v) $sec += pow(60, $k) * $v;
return $sec;
}
demo
$time = 00:06:00;
$timeInSeconds = strtotime($time) - strtotime('TODAY');
You can use the strtotime function to return the number of seconds from today 00:00:00.
$seconds= strtotime($time) - strtotime('00:00:00');
In pseudocode:
split it by colon
seconds = 3600 * HH + 60 * MM + SS
Try this:
$time = "21:30:10";
$timeArr = array_reverse(explode(":", $time));
$seconds = 0;
foreach ($timeArr as $key => $value)
{
if ($key > 2) break;
$seconds += pow(60, $key) * $value;
}
echo $seconds;
Simple
function timeToSeconds($time)
{
$timeExploded = explode(':', $time);
if (isset($timeExploded[2])) {
return $timeExploded[0] * 3600 + $timeExploded[1] * 60 + $timeExploded[2];
}
return $timeExploded[0] * 3600 + $timeExploded[1] * 60;
}
function time2sec($time) {
$durations = array_reverse(explode(':', $item->duration));
$second = array_shift($durations);
foreach ($durations as $duration) {
$second += (60 * $duration);
}
return $second;
}
echo time2sec('4:52'); // 292
echo time2sec('2:01:42'); // 7302
On Windows 32 bit PHP version: 7.2.31 i get some error on all versions posted here.
If the time was 00:00:00 or 00:00:00 the zeros 00 were returned and used as "" empty string, and calculation with empty string returns error "A Non WELLNUMERIC blabla.
This Works also with more then 24hours:
function TimeToSec(string $time) {
$timearray = explode(":",$time);
$hours = (int)$timearray[0];
$minutes = (int)$timearray[1];
$seconds = (int)$timearray[2];;
//echo "Hours: " . $hours ."<br>";
//echo "minutes: " . $minutes ."<br>";
//echo "seconds: " . $seconds ."<br>";
$value = ($hours * 3600) + ($minutes * 60) + $seconds;
return $value;
}
echo TimeToSec("25:00:30");
<?php
$time = '21:32:32';
$seconds = 0;
$parts = explode(':', $time);
if (count($parts) > 2) {
$seconds += $parts[0] * 3600;
}
$seconds += $parts[1] * 60;
$seconds += $parts[2];
On our site, we have a lot of swimming times that we would like to convert to seconds. i.e. 1:23:33.03 or 58:22.43. Is there a PHP function that can do this? A MySQL function?
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_time-to-sec
mysql> SELECT TIME_TO_SEC('22:23:00');
-> 80580
mysql> SELECT TIME_TO_SEC('00:39:38');
-> 2378
function time2seconds($time='00:00:00')
{
list($hours, $mins, $secs) = explode(':', $time);
return ($hours * 3600 ) + ($mins * 60 ) + $secs;
}
From here.
MySQL also has TIME_TO_SEC()
so if mysql without fractions not appropriate solution - here is another mine
$time = '1:23:33.03';
$parts = explode(':', $time);
$seconds = 0;
foreach ($parts as $i => $val) {
$seconds += $val * pow(60, 2 - $i);
}
echo $seconds;
Use the following program for converting time to seconds in php.
<?php
function time_to_sec($time) {
$hours = substr($time, 0, -6);
$minutes = substr($time, -5, 2);
$seconds = substr($time, -2);
return $hours * 3600 + $minutes * 60 + $seconds;
}
$result=time_to_sec('12:25:59');
echo $result;
?>
$current_time_in_seconds="01:21:44.24";
list($hours, $minutes, $seconds) = explode(":", $current_time_in_seconds);
$current_time_in_seconds=$hours*3600+$minutes*60+$seconds;
will get from
01:21:44.24
to
4904.24
Strtotime is what you need. You get a Unix timestamp which in this case would be the number of seconds.
I am a little confused but I think that you mean. 1 hour, 23 minuts and 23.03 seconds.
this is not difficult to do. I made this PHP function that does that. My php server doesn't want to start up, so I couldn't test it, but I think it should work.
function ConvertTimeToSeconds($T)
{
$exp = explode(":",$T);
$c = count($exp);
$r = 0;
if ($c == 2)
{
$r = (((int)$exp[0]) * 60) + ((int)$exp[1]);
}else{
$r = (((int)$exp[0]) * 3600) + (((int)$exp[1]) * 60) + ((int)$exp[2]);
}
return $r;
}
ConvertTimeToSeconds("1:23:33.03");