Milliseconds format php - php

I got this number: 116041 (is it in milliseconds).
And i want to transform to something like this minuts:seconds:miliseconds
Theoretically that number should transform to something like: 1:56:xx
And I'm trying this code:
$diff = 116041;
$date = date("i:s:u",$diff);
echo $date;
But I'm getting this output:
14:01:000000

date() takes a timestamp integer. The value you are supplying equals Friday, January 2, 1970 8:14:01 AM. Notice the 14:01? That is what you are getting using date("i:s:u",$diff);
Go to Epoch Converter and enter 116041 into the field and you can see it for yourself.

This should be pretty simple math.
// Get the minutes (60000ms per minute)
$milliseconds = 116041;
$minutes = floor(116041 / 60000);
// Find the remaining milliseconds
$milliseconds = $milliseconds % 60000;
// Continue to seconds calculation...

In this case, the date or DateTime route is more complicated. Just do some simple math...
$s = 116041 / 1000;
printf("%d:%02.3f", intdiv($s, 60), fmod($s, 60));

You can try this method. please let me know if it of any help to you.
<?php
function formatMilliseconds($milliseconds) {
$seconds = floor($milliseconds / 1000);
$minutes = floor($seconds / 60);
$hours = floor($minutes / 60);
$seconds = $seconds % 60;
$minutes = $minutes % 60;
$milliseconds = $milliseconds % 1000;
$format = '%u:%02u:%02u.%02u';
$time = sprintf($format, $hours, $minutes, $seconds, $milliseconds);
return rtrim($time, '0');
}
echo formatMilliseconds(2000202123);
?>

Related

Convert day hour-minute just to hour-minute

I have this code
echo $workedtime->format('d H:i'); //shows 01 10:01
I want it to show 34:01!
How can I do it?
You'll need two variables for this, where one of them is for when you started working and the second one is for when you finished work.
This could easily be solved by getting the date in UNIX timestamp for each variables.
function timeAgo($startTime, $endTime){
$seconds = strtotime($endTime) - strtotime($startTime);
$minutes = ($seconds / 60) % 60;
$hours = floor($seconds / 60 / 60);
return $hours . ":" . $minutes;
}
$startTime = "2018-12-24 09:00:00";
$endTime = "2018-12-25 15:30:00";
echo timeAgo($startTime, $endTime);
This returns 30:30
If you're using DateTime, you can just output the DateTime as UNIX timestamp instead of using strtotime. (Assuming you pass the DateTime as arguments for the function).
function timeAgo($startTime, $endTime){
$seconds = $endTime->getTimestamp() - $startTime->getTimestamp();
$minutes = ($seconds / 60) % 60;
$hours = floor($seconds / 60 / 60);
return $hours . ":" . $minutes;
}
echo timeAgo($startTime, $endTime); // $startTime and $endTime has to be DateTime.

convert negative seconds into hh:mm:ss format

wanne have a function that convert positive and negative seconds in Timeformat h:i:s
So i have value like
$seconds= -41880;
Try with first function
function secToHR($seconds)
{
$hours = floor($seconds / 3600);
$mins = floor($seconds / 60 % 60);
$secs = floor($seconds % 60);
$time = sprintf('%02d:%02d:%02d', $hours, $mins, $secs);
return $time;
}
result is
$time = -12:-38:00
When i use a positive value for $seconds
$seconds = 100380
then result is like
$time = 27:53:00
what is correct
Then second function
function secToHR2($seconds)
{
$time = gmdate("h:i:s", abs($seconds));
if ($seconds < 0) {
$time = '-' . $time;
}
return $time;
}
for
$seconds= -41880
result
$time = -11:38:00
wich is correct
but for
$seconds = 100380
result is now
$time : 03:53:00
which is now wrong
Does someone have the clue which functions i need and modified so that it is working correct for positive and negative values
gmdate() (and the other date-time functions as well) represents the number of seconds passed as argument as a date, not as a number of hours, minutes and seconds. It never returns a value greater than 23 for h and so on.
Combine the logic of the two functions: use the code of secToHR() to format the absolute value of its argument and the logic of secToHR2() to handle the sign.
function secToHR($seconds)
{
// Separate the sign and the absolute value of $seconds
$sign = '';
if ($seconds < 0) {
$sign = '-';
$seconds = -$seconds;
}
// Compute the components
$secs = $seconds % 60;
$minutes = ($seconds - $secs) / 60;
$mins = $minutes % 60;
$hours = ($minutes - $mins) / 60;
// P
return sprintf('%s%02d:%02d:%02d', $sign, $hours, $mins, $secs);
}
The easiest thing to do is to
extract any negative sign
call your function that's only defined for positive values (secToHR)
reinsert the negative sign if appropriate

PHP handle time value over 24:00H

I'm addditioning time value of a schedule.
When The value go over 24:00 I'm begining to have a problem..
Here is a simple example of what i'm trying to do.
$now = strtotime("TODAY");
$time_1 = strtotime('08:00:00') - $now;
$total = $time_1 * 5;
$total = $total + $now;
echo date('H:i', $total);
The echo value is 16:00:00
But it should be 40:00:00
24:00:00 + 16:00:00 = 40:00:00
So I understand that this is 1 day and 16 hours.
How can I echo 40:00:00
Below is your example code working the way you want.
As others have mentioned, you have to do the math yourself for cases like this.
<?php
$now = strtotime("TODAY");
$time_1 = strtotime('08:00:00') - $now;
$total = $time_1 * 5;
$secs = $total%60;
$mins = floor($total/60);
$hours = floor($mins/60);
$mins = $mins%60;
printf("%02d:%02d:%02d", $hours, $mins, $secs);
You can't. date() is intended to produce VALID date/time strings. 40 is not something that would appear in a normal time string. You'll have to use math to generate that time string on your own:
$seconds = $total;
$hours = $seconds % 3600;
$seconds -= ($seconds * 3600);
$minutes = $seconds % 60;
$seconds -= ($seconds * 60);
$string = "$hours:$minutes:$seconds";
The date function is for dates and times, not durations. Since the time is never "40:00", it will never return that string.
You can look into using the DateTimeInterface to get what you want, but it might be simpler just to do the math yourself.
$seconds = $total;
$minutes = (int)($seconds/60);
$seconds = $seconds % 60;
$hours = (int)($minutes / 60);
$minutes = $minutes % 60;
$str = "$hours:$minutes:$seconds";

Subtract Time String in PHP

Say I have the following as a string:
$timeLeft = 00:02:30
I want to be able to subtract 1 second, turning it into 00:02:29.
I have tried
$timeLeft - 1;
But it does nothing.
How can I make it so I can subtract seconds from a string?
You need to convert it to a time, subtract 1 second and reformat, e.g.:
$timeLeft = '00:02:30';
$time = DateTime::createFromFormat('H:i:s', $timeLeft);
$time->sub(new DateInterval('PT1S'));
$timeLeft = $time->format('H:i:s');
Below is dirty code that performs the transformation "manually" by converting the time into seconds in case PHP 5.3+ is not available. It'll certainly misbehave it the number of seconds subtracted is greater than the total.
$timeLeft = '00:02:30';
list($hours, $minutes, $seconds) = explode(':', $timeLeft);
$seconds += $hours*3600 + $minutes*60;
unset($hours, $minutes);
$seconds -= 1; //subtraction
$hours = floor($seconds/3600);
$seconds %= 3600;
$minutes = floor($seconds/60);
$seconds %= 60;
$timeLeft = sprintf("%'02u:%'02u:%'02u", $hours, $minutes, $seconds);
Using strtotime is a good practical solution, but you have to watch out for DST changes:
$tz = date_default_timezone_get(); // save old timezone
date_default_timezone_set('UTC'); // switch to UTC
echo date('H:i:s', strtotime($timeleft) - 1); // perform calculation
date_default_timezone_set($tz); // restore old setting

Does not get the time difference correct in PHP

$time1 = "01:00";
$time2 = "04:55";
list($hours1, $minutes1) = explode(':', $time1);
$startTimestamp = mktime($hours1, $minutes1);
list($hours2, $minutes2) = explode(':', $time2);
$endTimestamp = mktime($hours2, $minutes2);
$seconds = $endTimestamp - $startTimestamp;
$minutes = ($seconds / 60) % 60;
$hours = round($seconds / (60 * 60));
echo $hours.':'.$minutes;
exit;
Outputs 4:55, should be 3:55 ?
Whats wrong here? If it is 01:00 and 02:00, it works fine, but not with the above?
Use floor instead of round...
Or just cast to integer.
$hours = (int) ($seconds / (60 * 60));
Too many calculations when PHP can do it for you with also reducing possibility of error
$time1 = Datetime::createFromFormat("h:i", "01:00");
$time2 = Datetime::createFromFormat("h:i", "04:55");
$diff = $time1->diff($time2);
var_dump($diff->format("%h %i"));
Output
string '3:55' (length=4)
You can also save yourself some time by using strtotime:
$time1 = strtotime("01:00");
$time2 = strtotime("04:55");
$seconds = $time2-$time1;
$minutes = ($seconds / 60) % 60;
$hours = floor($seconds / (60 * 60));
echo $hours.':'.$minutes;
As mentioned, using floor will produce the result you need:
Result
3:55

Categories