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.
Related
$last = $today - (60*60*24*90); //90 days ago
Obviously i see the commented out line that says this is equivalent to 90 days but can someone explain exactly what each number is doing.
The asterisk is multiplying each number but it's confusing. Is that seconds or microseconds?
Thanks in advance.
$today is a timestamp, such as 1424800194
The timestamp is the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
Your example minuses 60 (seconds) * 60 (minutes) * 24 (hours) * 90 (days)
More info about timestamps: http://php.net/manual/en/function.time.php
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.
I need to input, 09 hours 20 minutes to strtotime and to get the corresponding timestamp. I tried out, strtotime("09 hours 20 minutes",0) but it gives me time passed in seconds in current day only. ie 33600.
I need to get the exact timestamp. ie time passed in seconds from 1-1-1970 to current day 09:20. Is there anyway? Any help will be appreciated.
Like Wong said in the first comment:
use "today" as offset
strtotime("09 hours 20 minutes", strtotime("today"));
$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 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.