Jump to content

Recommended Posts

//

I am trying to calculate the number of days between dates and have a major problem.  When I try the example below, it gives me exactly 201 days, yet the dates have different times, meaning that a full day is impossible.  If it matters, I am testing via xampp 1.7.3 with php version 5.3.1 with Windows XP.  Can anyone explain what I am doing wrong.  Can daylight savings play a role?

 

        $frmritedate7116    =    strtotime('2010-12-13 23:00');

        $frmritedate7156    =    strtotime('2010-05-27 00:00');

//

        $frmritedate7251    =    $frmritedate7156 - $frmritedate7116;

//

//

1274943600-1292310000=-17366400 // 86400 seconds in a day ( 201.00 )

//

//

//

Link to comment
https://forums.phpfreaks.com/topic/237735-strtotime-calculations-inaccurate/
Share on other sites

Yes, it's daylight savings that's confusing you. There were 17366400 seconds between those two dates. During that time an hour was repeated. Those 3600 seconds still happened, but on the clock you "lost" an hour.

 

That's why you shouldn't use seconds to calculate differences to a preciseness of an hour: because your concept of time isn't always how time actually is.

You still can do it, if you really want, but you need to take daylight savings into account by adding or subtracting 3600 seconds to get the kind of values you want: -3600 for each fall DST boundary and +3600 for each spring DST boundary.

Have you tried the dateTime class?

 

<?php
$datetime1 = new DateTime('2010-12-13 23:00');
$datetime2 = new DateTime('2010-05-27 00:00');
$interval = $datetime2->diff($datetime1);
echo $interval->format('%R%a days %h:%i:%s'); //%a is broken on Windows servers.  You will have to use years, months, and days.
?>

 

Manual

Hmmm, looks promising, but I am testing via xampp on my PC so it is difficult to confirm.  One day I will have to upload the pages and give it a test.  Thanks for the info though.  I will get back, probably in a few weeks whether it worked or not.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.