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
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"));

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.