In php i have two times - 11:00:00 and 12:45:00. I want to get the difference between them in minutes, in this case 105 minutes. Whats the best way that can be done?
Thank you!
Here you go:
( strtotime('12:45:00') - strtotime('11:00:00') ) / 60
strtotime() is a very useful function. It returns the Unix timestamp for a wide variety of times and dates. So, if you take the two timestamps, and subtract them, then you have the difference in seconds. Divide by 60 to get the minutes.
$time_diff = strtotime('2013-03-13 12:45:00') - strtotime('2013-03-13 11:00:00');
echo $time_diff/60;
I just kept dates as not sure if I keep the time part only it would return the correct diff or not.
EDIT
I just tested it works without date too ...
$time_diff = strtotime('12:45:00') - strtotime('11:00:00');
echo $time_diff/60;
So to answer you question - strtotime() returns a timestamp (the number of seconds since January 1 1970 00:00:00 UTC) so you simply divide it by 60 to convert it result into minutes.
Related
I am trying to calculate the time difference in hours between to times in format hh:mm. The problem is the data is in both 12 hour and 24 hour.
eg. 23:55 - 00:10 , 12:10 - 01:02, 10:50 - 11:10 etc..
I tried the method given here PHP - hours difference (HH:MM format)
I need a function with all conditions to handle the 24h, 12h to calculate the time difference in hours.
Many thanks.
The code in linked solution is fine except over midnight case. If you assume positive difference (start before end) - you may add 1 day (in seconds) when it appears to be negative. Just change return statement to:
return ($timeDiff > 0) ? $timeDiff : $timeDiff + 24*60*60;
I'm trying to figure out how to compare against date(). I'm following along in a tutorial about how to use this function to compare the current time against the time a cache file was last modified. In the tutorial, the author uses "10800" as 3 hours and the code looks something like:
(filemtime($cache) < (time()-10800))
I have no problem understanding how this comparison works but I just don't get how the the expression of time, "10800", is formatted.
Just for the record I spent a solid 15 minutes looking for an answer so I'm not just being ignorant of Google haha.
Thanks!
10800 is in seconds..
all unix timestamps are measured in seconds since the epoch... 1 being the first second of 1970.
This explains why when you have a bad strtotime value and you are interpreting it with date i.e.
date(strtotime("last tomorrowday"));
it ends up showing you 1969-12-31 ... strtotime is returning 0 and if 1 is the first second of 1970 then 0 will be interpreted as the last second of 1969
It's in seconds,
3 hours = 3 * 60 * 60 = 10800 seconds
As time function returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). You neeed to subtract 10800 from it to get timestamp of time before 3 hours.
$dateTime="2011-10-12 00:00:00";
echo $newDateTime =date("Y-m-d H:i:s", strtotime($dateTime.' -1 hours 30 minutes'));
The result of above code is '2011-10-11 23:30:00'. However, the correct answer should be
2011-10-11 22:30:00.
Is there anying wrong in the code and can anyone help me?
Many thanks
23:30 is the expected result (once you know what is happening).
The relative parts of the string (-1 hours 30 minutes) are processed separately as -1 hours and 30 minutes. They are two instances of the number space? (unit | 'week') format as described in the Relative Formats documentation.
Because of this the cumulative relative change in the time is only -30 minutes, which from midnight gives 23:30.
To get the effect that you desire, either:
use a single relative statement (e.g. -90 minutes)
make your original minutes statement negative as -1 hours -30 minutes
or, use the special ago format as 1 hours 30 minutes ago
See http://php.net/datetime.formats.relative for more details.
date functions aren't fully daylight savings aware. Try using dateTime objects instead
I need to get the difference of hours from between two timestamps. Can you tell me how can it be done using php?
Thanks in advance.
Regards,
$hours = (abs(strtotime($timestamp1)-strtotime($timestamp2)) / 60) / 60;
You can feed the values of timestamps into strtotime(), this will get you the UNIX TIME STAMP in seconds since 1970. It'll be two large integer second values. So then do a subtraction between the two values and convert that into whatever you're looking to get. e.g.
minutes, hours, days, weeks, etc... by doing normal *60, *60, *24, *7 computation
Use strtotime to convert the timestamps into seconds, subtract them, use abs to get the absolute value (no negative numbers here!), divide by 3600 seconds (1 hour), and then round to the precision you'd like.
$difference = round(abs(strtotime($stamp_one) - strtotime($stamp_two)) / 3600, 2);
I am trying to calculate the age of something in hours.
$data['record'] is a mysql NOW() timestamp in a DATETIME field.
$data['record'] is 20 minutes old, when I do :
$minutes= date('i',(strtotime("now")-strtotime($data['record'])));
$minutes returns 20 properly, however for some reason when I try $hours it returns '5'.
$hours = date('g',(strtotime("now")-strtotime($data['record'])));
This does not make sense, as $hours should be returning 0 as the record is less than 60 minutes old...
When I checked the value of "strtotime("now")-strtotime($data['record'])" it is equal to '980'. Help!
Please compare the output of strtotime("now") of php and select now(); in sql. I think there is a timezone problem hidden here.
As you said, strtotime("now")-strtotime($data['record']) returns 980, which should be in minutes. 960 is divideable by 60 and comes out at 16 hours, so 980 is 16 hours 20 minutes - the 20 minutes are exactly what you are looking for. You'll need to adjust either instance to use the time of the other - I would go with always using UTC. If you need to display it, parse it appropiately and output the local time.
Please See: http://php.net/manual/en/function.date.php
When the $format parameter="g", it returns a value 1-12.
Date will not quite work like you're expecting it to.
Date takes a time stamp (# of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)), and converts that into a legible time format. Essentially, with a value of 980, you are going to get January First at midnight + 980 seconds (roughly January 1 1970 00:16:20 GMT. When you convert for the time zone difference, (chances are, about 5 hours difference) that's how you get five.
To fix this, simply take 980, and divide by 60 to get minutes, then divide by 60 again to get hours, so:
$hours = ((strtotime("now")-strtotime($data['record'])) / 60) / 60;
There's no need for date, as you need a relative time, not an absolute time.