pepelepew1962 Posted May 28, 2011 Share Posted May 28, 2011 // 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 ) // // // Quote Link to comment https://forums.phpfreaks.com/topic/237735-strtotime-calculations-inaccurate/ Share on other sites More sharing options...
mikesta707 Posted May 28, 2011 Share Posted May 28, 2011 If I were to guess, I would say daylight savings time is causing the unexpected behavior. The math is correct, and I can't imagine that the timestamps being returned are wrong Quote Link to comment https://forums.phpfreaks.com/topic/237735-strtotime-calculations-inaccurate/#findComment-1221710 Share on other sites More sharing options...
requinix Posted May 28, 2011 Share Posted May 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237735-strtotime-calculations-inaccurate/#findComment-1221716 Share on other sites More sharing options...
pepelepew1962 Posted May 28, 2011 Author Share Posted May 28, 2011 Lovely, thanks for your response though. Is there any trick, perhaps calculating via Julian date or something else that would work? These calculations are critical for production as the product is food and time cut-offs important. Quote Link to comment https://forums.phpfreaks.com/topic/237735-strtotime-calculations-inaccurate/#findComment-1221724 Share on other sites More sharing options...
pepelepew1962 Posted May 29, 2011 Author Share Posted May 29, 2011 Ok, if all else gfails go to pencil and paper. I got my calendar out and tapped each day from Dec 13 ( 23:00 hrs ) back and my finger stopped at 200 days on May 17, all being equal ( 23:00 hrs). Where did the 1 whole day go? Quote Link to comment https://forums.phpfreaks.com/topic/237735-strtotime-calculations-inaccurate/#findComment-1221731 Share on other sites More sharing options...
jcbones Posted May 29, 2011 Share Posted May 29, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/237735-strtotime-calculations-inaccurate/#findComment-1221744 Share on other sites More sharing options...
pepelepew1962 Posted May 29, 2011 Author Share Posted May 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237735-strtotime-calculations-inaccurate/#findComment-1221752 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.