Jump to content

Strange behavior of date function


abdfahim

Recommended Posts

Dear Developers,

 

I am facing a starnge problem. I wan to subtract 2 days from a given date and so I use the code below.

$dateto = strtotime("2010-03-30");
echo Date("Y-m-d",$dateto-(2*86400));

 

This works fine for everything accept 29th March, 2010 and 30th March, 2010 !!!

 

That is, the output of the simple program above is ..

 

2010-03-25 when $dateto=2010-03-27

2010-03-26 when $dateto=2010-03-28

2010-03-26 when $dateto=2010-03-29

2010-03-27 when $dateto=2010-03-30

2010-03-29 when $dateto=2010-03-31

2010-03-30 when $dateto=2010-04-01

2010-04-27 when $dateto=2010-04-29

2010-04-28 when $dateto=2010-04-30

 

Link to comment
https://forums.phpfreaks.com/topic/197602-strange-behavior-of-date-function/
Share on other sites

Well, I'm not quite sure why you are getting "those" results. I get different errors, but I know why - daylight savings time. If that is your problem you can fix this by setting the time to the middle of the day. And you can get the correct results with one function using a more "natural" conversion of "-2 day".

 

echo Date("Y-m-d", strtotime("2010-03-30 12:00:00PM -2 day"));

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.