$diff = strtotime(12:00:00) - strtotime(5:01:29);
echo date('H:i:s', $diff);
result is 12:01:29
Trying to get the result of 5 hrs 1 min 29 sec
I prefer to use the DateTime class for time comparison / addition / subtraction:
$dt1 = new DateTime();
$dt1->setTimestamp(strtotime('12:00:00am'));
$dt2 = new DateTime();
$dt2->setTimestamp(strtotime('5:01:29am'));
$interval = $dt1->diff($dt2);
echo $interval->format('%h hrs %i min %s sec');
Edit: with the format you gave in strtotime it was thinking 5:01am to 12pm - so it was giving a very different answer than what you wanted, so you need to specify am/pm
Related
I need to calculate the datetime difference in minutes using PHP. I am explaining my code below .
$date='10-03-2018 03:44 PM';
$endTime = strtotime($date);
$currentDate=date("d-m-Y h:i A");//10-03-2018 03:53 PM
$currentTime = strtotime($currentDate);
echo (round(abs($currentTime - $endTime) / 60,2));//25344617
Here I need to calculate the difference in minutes but the differnce value is more where the expected time difference should be 9 but as per my code I am getting the wrong value.
Let the PHP DateTime class with diff() method do the work with time calculations.
$now = '10-03-2018 03:53 PM'; // or use simply 'now' for current time
$endTime = '10-03-2018 03:44 PM';
$datetime1 = new DateTime($now);
$datetime2 = new DateTime($endTime);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%i minutes'); // 9 minutes
See it live: https://eval.in/969615
I have two Timestamps
2016-01-01 00:00:00
2016-01-02 23:59:59
Using PHP how can I calculate the number of hours and minutes between the two times and get the result as a decimal with 2 places after the .
currently I have this:
$Start = new DateTime($StartTime);
$Finish = new DateTime ($FinishTime);
$Interval = date_diff($Start,$Finish);
$Hours = $Interval->format('%h.%i');
But the result is incorrect if the user starts the timer on Day 1 and finishes on day 2.
You could multiply the number of days by 24 to convert them to hours, then sum the hours and concatenate the minutes:
$start = new DateTime('2016-01-01 00:00:00');
$end = new DateTime('2016-01-02 23:59:59');
$interval = $end->diff($start);
$days = $interval->format('%d');
$hours = 24 * $days + $interval->format('%h');
echo $hours.':'.$interval->format('%i');
You could format the DateTime as a UNIX timestamp, and then simply subtract to get the total seconds, and format the output with gmdate().
$Start = new DateTime($StartTime);
$Finish = new DateTime ($FinishTime);
$Interval = $Start->format('U') - $Finish->format('U');
$Hours = gmdate("H:i:s", $Interval);
Try this.
$Hours = $Interval->format('%a.%h.%i');
I want to calculate the time difference from now (lets say 18:30:00) till this evening at 20pm.
$today = date('Y-m-d', time());
$remain = strtotime($today. " 00:00:00 + 20 hours") - time();
$remain = date('H:i:s', $remain);
I get a result which is one hour larger (02:30:00) than the actual result (01:30:00). I tried setting time zones but it's always the same result.
Using the DateTime object, you can do this easily:
$d1 = new DateTime('2015-04-23 18:30');
$d2 = new DateTime('2015-04-23 20:00');
$interval = $d2->diff($d1);
echo $interval->format('%H:%i hours');
Is there a nice simple shorthand way of finding out how many seconds past midnight a certain datetime is? Not how many seconds it is away from now, the seconds past in that day
eg:
2015-04-10 00:00:00 should returns 0
2015-04-12 09:20:00 should returns 33600
2015-04-14 15:20:00 should returns 55200
Is there a nice short method of doing this?
Here is a simple code for you.
Any day code
<?php
$date = "2015-04-12 09:20:00";
$midnight = strtotime(date("Y-m-d 00:00:00", strtotime($date)));
$now = strtotime($date);
$diff = $now - $midnight;
echo $diff;
?>
Current day code
<?php
$midnight = strtotime("midnight");
$now = date('U');
$diff = $now - $midnight;
echo $diff;
?>
Maybe a more clean code would be to use strtotime("midnight", <EpochTime>);
<?php
$date = "2015-04-12 09:20:00";
$midnight = strtotime("midnight", strtotime($date));
$now = strtotime($date);
$diff = $now - $midnight;
echo $diff;
?>
A day is 24 hours is 60 minutes is 60 seconds is 1000 miliseconds, so timestamp % (24*60*60*1000) could work, maybe, at least if you are on the same time zone as the epoch php is using (or whatever you timestamp is coming from). It would work at least for nothing too demanding of accuracy. PHP uses your system's time, so many things will break the idea (change the clock, DST, leap seconds, etc)
I would personally use timestamp - startofday timestamp and calculate using the language's calendar instead.
I would like to compare a difference between two timestamps and a time in base 60 or 10.
More precisely if timestamp1-timestamp2 is longer (or no) than x hours and y seconds.
I'm using DateTime and DateInterval classes, but there isn't such a function, and i don't find a clean solution.
Thanks
$time1 = new DateTime("2011-01-26 01:13:30"); // string date
$time2 = new DateTime();
$time2->setTimestamp(1327560334); // timestamps, it can be string date too.
$interval = $time2->diff($time1);
echo $interval->format("%H hours %i minutes %s seconds");
Output
11 hours 32 minutes 4 seconds
$timestamp1 = strtotime('2012-01-26 14:00:00');
$timestamp2 = strtotime('2012-01-25 17:00:00');
if (abs($timestamp1 - $timestamp2) < 60 * 60 * 5 /* (5 hours) */) {
...
Convert both the timestamp and the real time to the UNIX timestamp. Then simply subtract to get the number of seconds difference.
With datetime objects:
$interval = $TempReceiptDateTime->diff($ShipDateTime);
echo $interval->format('%R%H:%I:%s days');
more