Jump to content

How To Offset In Php 28 Days Ago?


yoursurrogategod

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/270341-how-to-offset-in-php-28-days-ago/
Share on other sites

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

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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.