Jump to content

strtotime doesn't like October


Simmo

Recommended Posts

Hi

 

I use strtotime to add 7 dates into my database, so if the user selects a weeks holiday I can see what the next 6 days are and store them in a table.  This was working well, so I thought.  Until I put the script on another server. For some reason it wouldn't increment from Oct 30 2010 to Nov 5, it would go 31, 01, 01, 01, 01, 01.  I asked them to update PHP which they did, this didn't make any difference.  Though I am in England I asked them to change the time zone to that of my testing server which was Chicago.  This then worked for Oct 2010, but made me worry that there could be a problem, so I ran the following script to see how the dates would look in October for the next 5 years:

 

<?phpecho "Date Test";echo "<br />";for($k=10; $k<16; $k++){for($j=01; $j<13; $j++){	$varDate = "29-".$j."-20".$k."";	for($i=1; $i<7; $i++){	$varDate = date('Y-m-d',(strtotime($varDate) + 1*24*3600));	echo $varDate."<br />";	}	echo "<br />";}}?>

 

On both servers there is now problem in October 2014 and 2015, here are the results of those month’s:

2014-10-30

2014-10-31

2014-11-01

2014-11-02

2014-11-02

2014-11-02

and

2015-10-30

2015-10-31

2015-11-01

2015-11-01

2015-11-01

2015-11-01

 

Any help would be appreciated.

 

Thanks

Link to comment
Share on other sites

Hmmm...  In the United States, what happens the first Sunday in November?  You will be better off using PHP to calculate dates instead of using your own addition.  PHP date/time functions take timezones, leap years and other differences (like what I am hinting at) into account.

 

$varDate = date('Y-m-d', strtotime('+1 day', strtotime($varDate)));

 

Or something similar, or mktime().

Link to comment
Share on other sites

Only because your server is set to a timezone that doesn't observe DST, such as GMT or UTC, etc...  Which is my point about the math.

 

echo date_default_timezone_get()."\n";

$date = "2014-10-31";
$i=1;
while($i< {
echo $date . "\n";
$date =date("Y-m-d", (strtotime($date) + 86400));
$i++;
}

America/Chicago
2014-10-31
2014-11-01
2014-11-02
2014-11-02
2014-11-02
2014-11-02
2014-11-02

 

date_default_timezone_set('UTC');
echo date_default_timezone_get()."\n";

$date = "2014-10-31";
$i=1;
while($i< {
echo $date . "\n";
$date =date("Y-m-d", (strtotime($date) + 86400));
$i++;
}

UTC
2014-10-31
2014-11-01
2014-11-02
2014-11-03
2014-11-04
2014-11-05
2014-11-06

 

 

Link to comment
Share on other sites

Actually, the errors being shown in the results are likely due to incorrect values in the time-zone/dst database, rather than the fact that the time zone is one where dst affects the result (there would at most be a 1 hour error when the day changes, not the same result for several days.)

Link to comment
Share on other sites

Actually, the errors being shown in the results are likely due to incorrect values in the time-zone/dst database, rather than the fact that the time zone is one where dst affects the result (there would at most be a 1 hour error when the day changes, not the same result for several days.)

 

No, if the system subtracts an hour and then you add 24 then you have the same day.  Do it again and you have the same day, and so on.

Link to comment
Share on other sites

Thanks for your comments.

 

I should have started with the basics first and used the +1 day, I seemed to have over complicated things.  All solutions have been great, adding date_default_timezone_set('UTC'); sorted the problem out straight away, so I'll use that.  It’s been interesting to see how time zone settings need to be considered.  I have also learned that the first Sunday in November is when the American clocks change.

 

Thanks again

 

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.