i use this url for time to minutes conversion but extra hour are shown where there is no hour in time.
my code is
<?php
$seconds=strtotime('00:11:10').'<br/>';
echo gmdate('H:i:s',$seconds).'<br/>';
$H = floor(($seconds / 3600));
$i = ($seconds / 60) % 60;
$s = $seconds % 60;
echo sprintf("%02d:%02d:%02d", $H, $i, $s);
?>
the output showing 389496:11:10 //it should 00:11:10
what the problem is...??
I really getting pain on this any help would be appreciated .
This is the snippet I use for HH:MM:SS to minutes conversion.
<?php
$str_time = "00:11:10";
$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;
$minutes = $time_seconds / 60;
echo $minutes;
The output, as you expect, is 11.166666666667.
If the time is 1 hour, 20 minutes and 10 seconds, the output is then: 80.166666666667
I'd venture to say because you append <br> to the $seconds, and then use it with math functions like $seconds/3600 ... etc .. remove the <br> and add it later, or remove the <br> altogether
$seconds=strtotime('00:11:10');
echo gmdate('H:i:s',$seconds).'<br/>';
$H = floor(($seconds / 3600));
$i = ($seconds / 60) % 60;
$s = $seconds % 60;
echo sprintf("%02d:%02d:%02d", $H, $i, $s);
Related
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);
?>
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
I wanted to find the End Time of a incoming visitor . I have recorded the Intime & duration in My DB through an API , But the API does not provide the option for End Time. It looks like i have to calculate the End time, from ( Intime & Duration ) . However i did find the end time. But it is taking too much time to process the code and i am geting error like Fatal error: Maximum execution time of 600 seconds exceeded in D:\xampp-portable\htdocs\logmein\ on line 68 .
Please look into my code and tellme a way to customize my Code in a very easier way.
Start time-11/19/2014 4:57 AM,Duration-1423
I want :Start time:2014-11-20 12:48:17,
End Time:2014-11-20 13:07:17,
Duration-00:19:35
<?php
$WaitingTime=1423;
$i = ($WaitingTime / 60) % 60;
$duration = sprintf('%02d:%02d:%02d', ($WaitingTime / 3600), ($WaitingTime / 60 % 60), $WaitingTime % 60);
$timestamp = strtotime($strat_time);
$chat_start=date("Y-m-d H:i:s", $timestamp);
$time = new DateTime($chat_start);
$time->add(new DateInterval('PT' . $i . 'M'));
$chat_end = $time->format('Y-m-d H:i:s');
?>
Something like the following achieves this:
<?php
$durationSeconds = 1423;
$startDateString = '11/19/2014 4:57 AM';
$startTime = new DateTime($startDateString);
$startString = $startTime->format('Y-m-d H:i:s');
$duration = new DateInterval('PT'.$durationSeconds.'S');
$hours = floor($durationSeconds / 3600);
$minutes = floor(($durationSeconds - ($hours * 3600)) / 60);
$seconds = $durationSeconds % 60;
$durationString = $hours.':'.$minutes.':'.$seconds;
$startTime->add($duration);
$endString = $startTime->format('Y-m-d H:i:s');
Thanks all,This is my answer.
<?php
$WaitingTime=1423;
$strat_time="11/19/2014 2:49:23";
$dateinsec=strtotime($strat_time);
$newdate=$dateinsec+$WaitingTime;
$chat_start=date("Y-m-d H:i:s", $dateinsec);
$chat_end= date('Y-m-d H:i:s',$newdate);
$duration = sprintf('%02d:%02d:%02d', ($WaitingTime / 3600), ($WaitingTime / 60 % 60), $WaitingTime % 60);
<?
This is my answer2
$time1 = strtotime("$call_start");
$time2 = strtotime($call_end);
$diff = $time2 - $time1;
$ath = sprintf('%02d:%02d:%02d', ($diff / 3600), ($diff / 60 % 60), $diff % 60);
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";
$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