Error on strtotime() - php

$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

Related

Comparing and checking PHP Date()

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.

PHP - time minus time to minutes

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.

How to get strtotime() with written time format?

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"));

strtotime returning unexpected results, relative timestamps

I am using strtotime to get some relative timestamps using a string description of the amount of time to shift forward or backward, ie "+1 hour, 15 minutes", "-2 hours, 45 minutes".
This works perfectly:
$ds = strtotime('02/20/11 09:30:00 -1 hour');
However, if it is "-1 hour, 15 minutes", strtotime seems to be subtracting the hour, then adding the 15 minutes.
I tried it with a comma: http://codepad.org/3dq7c2GM
I came here, saw a previous question about this, and tried it without a comma: http://codepad.org/G8gm8E84
Still no luck. I've tried using the relative parameter: http://codepad.org/nwZ9ZqOv Nothing.
I found my own solution, which is pretty unintuitive, and I am not certain if I am missing something or perhaps even approaching this incorrectly. The working version ends up being:
$ds = strtotime('02/20/11 09:30:00 -1 hour -15 minutes');
http://codepad.org/O89S3mCj
Am I missing something here? No mention of this kind of behavior in the docs. Also, is there a better way to obtain such relative timestamps using string-based durations?
EDIT
Edited to add the source of these strings - they are coming from the user interface via AJAX. There are form controls that have +/- buttons to increment/decrement the duration in a display field by 15 minute intervals. When they change, the AJAX request fires these values to the server, to be turned into epoch timestamps via strtotime, which are in turn stored in the database.
I haven't observed that behavior before, but maybe it's because I've always converted my intervals to minutes. I.e.,
$ds = strtotime('02/20/11 09:30:00 -75 minutes');
Where does your time come from or where do you use it after? You might be able to use the MySQL Date manipulations if there's some SQL involved in this.
It makes great sense - it is a interpreter after all.
<?php
$ds = strtotime('02/20/11 09:30:00 -1 hour, 15 minutes');
echo $ds."\n";
echo date('m/d/Y h:i:s a', $ds)."\n";
?>
that one suctracts 1 hour and adds 15 minutes to your time, so if the 15 minutes is turned into negative (as you did, it will do it as you wish)
I would never use strtotime personally - i prefer working with seconds since epoch :-)

Trouble getting age in hours

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.

Categories