yoursurrogategod Posted November 5, 2012 Share Posted November 5, 2012 (edited) Hi, I'm confused about this code: <?php //phpinfo(); $twentyEightDaysOffset = 28; echo "Since time immemorial: " . time() . "<br><br>"; echo "Offset 28 days ago: " . (time() - ($twentyEightDaysOffset * 24 * 60 * 60)) . " and now in days: " . (date('Y-m-d', time() - ($twentyEightDaysOffset * 24 * 60 * 60))) . "<br><br>"; echo "A slightly more clever way of doing this: " . (date('Y-m-d', strtotime('-28 day'))) . "<br><br>"; ?> When I run this code, this is what I get: Since time immemorial: 1352153998 Offset 28 days ago: 1349734798 and now in days: 2012-10-09 A slightly more clever way of doing this: 2012-10-08 Now, here is my question. Why is it that when I do (time() - (28 * 24 * 60 * 60)), I get a different date than when I run strtotime('-28 day')? And why is it that when I set the strtotime('-9 day) and $twentyEightDaysOffset to 9, that's when the drift in dates begins? Also, is there a way to post PHP code with indentations so that it looks better? Edited November 5, 2012 by yoursurrogategod Quote Link to comment Share on other sites More sharing options...
Barand Posted November 6, 2012 Share Posted November 6, 2012 Hmm. I get Offset 28 days ago: 1349746886 and now in days: 2012-10-09 A slightly more clever way of doing this: 2012-10-09 Quote Link to comment Share on other sites More sharing options...
Barand Posted November 6, 2012 Share Posted November 6, 2012 PS I wonder if it's a timezone thing? If I use "Y-m-d H:i:s" it gives Offset 28 days ago: 1349747458 and now in days: 2012-10-09 02:50:58 A slightly more clever way of doing this: 2012-10-09 01:50:58 Quote Link to comment Share on other sites More sharing options...
yoursurrogategod Posted November 6, 2012 Author Share Posted November 6, 2012 (edited) PS I wonder if it's a timezone thing? If I use "Y-m-d H:i:s" it gives Offset 28 days ago: 1349747458 and now in days: 2012-10-09 02:50:58 A slightly more clever way of doing this: 2012-10-09 01:50:58 That did not occur to me. This is what I get: Since time immemorial: 1352214778 Offset 28 days ago: 1349795578 and now in days: 2012-10-09 17:12:58 A slightly more clever way of doing this: 2012-10-09 16:12:58 And I checked in my php.ini file and the timezone was in Europe/Berlin, I changed that to America/New_York. I'll go with the "clever" approach (easier to understand). Edited November 6, 2012 by yoursurrogategod Quote Link to comment Share on other sites More sharing options...
Barand Posted November 6, 2012 Share Posted November 6, 2012 If you run it near midnight, one will be in one day and one in the next. Quote Link to comment Share on other sites More sharing options...
Zane Posted November 6, 2012 Share Posted November 6, 2012 DST was just this last Sunday you know? Still doesn't explain why the time starts to drift 9 days ago, but still.. it most likely has everything to do with it. Quote Link to comment Share on other sites More sharing options...
jcbones Posted November 7, 2012 Share Posted November 7, 2012 I like to use the DateTime object, not sure if it changes these problems though. <?php $date = new DateTime(); $date->sub(new DateInterval('P28D')); echo 'The correct way to do it is: ' . $date->format('Y-m-d'); Quote Link to comment 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.