$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
Related
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.
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 trying to get the difference between two date-times and return it as a minute. Date & Time are taken in date("Y-m-d H:i:s") format. But it seem i can't get it right. I did it
$time=date("Y-m-d H:i:s");
$time=date("2014-01-13 08:18:25");
$interval = $time->diff($login_time);
$elapsed = $interval->format(%i minutes);
echo $elapsed;
And This is showing a massage
"Call to a member function diff() on a non-object"
As I am not good enough with date formatting. So Please help me.
What is the way to go about this?
Try this:
$date1 = new DateTime('2013-01-13 04:10:58');
$datediff = $date1->diff(new DateTime('2013-09-11 10:25:00'));
echo $datediff->i;
For more details see this link : http://www.php.net/manual/en/book.datetime.php
Get difference is minutes between two dates:
$now = new DateTime;
$before = new DateTime('2014-01-10 08:18:25');
$diff = $now->diff($before);
$elapsed = $diff->days * 24 * 60 + $diff->h * 60 + $diff->i;
demo
Use the below code for getting time in all expected parameter.
$time1=date("Y-m-d H:i:s");
$time2=date("2014-01-13 08:18:25");
$seconds = strtotime($time1) - strtotime($start);
$year = floor(($seconds)/(60*60*24*365)); $months = floor($seconds /
86400 / 30 ); $week = floor($seconds / 604800); $days =
floor($seconds / 86400); $hours = floor(($seconds - ($days * 86400))
/ 3600); $minutes = floor(($seconds - ($days * 86400) - ($hours *
3600))/60); $seconds = floor(($seconds - ($days * 86400) - ($hours *
3600) - ($minutes*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";
Im having a a bit strange problem, Im having this code and on output it adds always one hour more if the second time has 30 or more minutes.
$time1 = '12:00';
$time2 = '13:30';
list($hours, $minutes) = explode(':', $time1);
$startTimestamp = mktime($hours, $minutes);
list($hours, $minutes) = explode(':', $time2);
$endTimestamp = mktime($hours, $minutes);
$seconds = $endTimestamp - $startTimestamp;
$minutes = ($seconds / 60) % 60;
$hours = round($seconds / (60 * 60));
Whats happening here?
Remember the math. Rounding up everything in interval [0.5;1) equals to 1.
round(0.5) = 1
That's why you've +1 hour in case of minutes in [30;60].
Instead of using round use intval as $seconds / (60 * 60) expression always returns a float and we need only the integer part of that result