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 :-)
Related
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 wish to calculate the difference b/w 2 times in min:sec format . so is my approch correct
date("i:s",(strtotime($User['end_time']) - strtotime($User['start_time'])));
You may get the problems with timezones on some servers.
A bettter way would be using UTC timezone for calculation:
$date = new DateTime('', new DateTimeZone('UTC'));
$date->setTimestamp(strtotime($User['end_time']) - strtotime($User['start_time']));
echo $date->format('i:s');
Another thing, if they are different in exactly 1 hour, the result will be 00:00
strtotime($User['end_time']) - strtotime($User['start_time']) gives you the difference in seconds. Then you pass it to date, so you get the minute and second of the date whose unix timestamp is that.
$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
Assume I have a funny video site that shows funny videos to users. Below every video, I want to have a statement that says "5 seconds ago", "31 minutes ago", "Yesterday", "2 days ago" based on a timestamp of the video and the current time.
The timestamp that I have is in the format: 2011-10-17 07:08:00.
I'm not too good with dates, but I'm guessing that I need to find the difference between the 2 date/time in seconds, then decide if its between 0sec & 60sec to display in seconds, or between 60sec & 3600sec to display in minutes, or between 3600sec & 3600x24sec to display in hours, between 3600x24sec & 3600x24x2sec to display yesterday, and so on... I believe I should be using strtotime() but I cant seem to find the current time as those solutions I found used new date() which does not seem to work!
How can I do this?
Btw, side question, when I insert 2011-10-17, 7:08PM EDT into a MySQL timestamp column, it gets converted to 2011-10-17 07:08:00 which is AM. How can I get it to be stored in the correct AM/PM?
You can use the DateTime functions of php.
$datetime1 = new DateTime('2011-10-17 07:08:00');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
From here on you can use some if-statements to output the time difference in another format (seconds, minutes, hours, month, etc.) depending on the actual time difference! The formats for the output are to find here
You can very easily use the DATEDIFF and TIMEDIFF functions of MySQL. Both together tell you exactly how much time has passed.
I am converting PHP (v 5.2.17)-based reports to SSRS.
I'm trying to write a query that subtracts dates like the php file does.
The date calculation either matches exactly, or it differs by exactly 3600 seconds
$timediff=strtotime(date("Y-m-d"))-strtotime("03/29/2007");
print $timediff; // 137635200
select (trunc(sysdate) - to_date('03/29/2007','MM/DD/YYYY'))*60*60*24 from dual
-- returns 137635200 - matches
$timediff=strtotime(date("Y-m-d"))-strtotime("11/23/2009");
print $timediff; // 53823600
select (trunc(sysdate) - to_date('11/23/2009','MM/DD/YYYY'))*60*60*24 from dual
-- returns 53827200 - doesnt match - off by 3600
I've searched stackoverflow and found the following example which sounds like it (11/23/2009 is a monday, and so is today 8/8, but it doesnt differ in the same way) PHP Strtotime erratic function
My assumption is that the php calculation is wrong, and the oracle is correct.
What say you? ;-)
Thanks!
Without seeing the exact values I suspect its because one of them is adjusting for daylight savings and the other is not. Most parts of the world change to/from daylight savings time during march. So in your first example both dates are in the same GMT offset, but in the second (November) they're not.
So one of your platforms (probably Oracle) is taking this one hour shift into account and the other is not.
3600 = an hour so could be timezone differences?
53827200 = ( 623 * 60*60*24 )
So 53827200 is exactly 623 days of 24 hours.
53823600 is 622.958333 days or 622 days (of 24 hours) plus 23 hours.
Both are right, for a given definition of right.