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?

Edited by yoursurrogategod
Link to comment
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

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 by yoursurrogategod
Link to comment
Share on other sites

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.