RE: strange behaviour with strtotime()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 31 January 2006 13:13, Barry Krein wrote:

> Ford, Mike wrote:
> 
> > 
> > Because 1 month from 31-Jan is 31-Feb -- which is taken to
> mean 02-Mar, hence the output of date("M" ...).
> > 
> > 2 months from now is 31-Mar, which is ok; 3 months from now
> would be 31-Apr, which would likewise be taken to mean 01-May.
> > 
> > This is all as expected -- no bug here.
> > 
> > Cheers!
> > 
> > Mike
> > 
> so the 31 feb would be 30 days right?
> and since +1 month would be 2nd march wouldnt be +2 month
> then 2nd april?
> Sinze its 2nd march +1 month
> 
> see ->
> echo strftime("%d %m %Y",strtotime ("+1
> Month",mktime(13,0,0,3,2,2006)))."\n";
> 
> ^^ gives 2nd april! ^^
> 
> echo "date 1:".strftime("%d %m %Y",strtotime ("+1
> Month",strtotime("+1 Month")))."\n"; 
> 
> ^^ gives 3rd april ^^

Uhm -- my mistake, sorry -- 31st Feb equates to 3rd March, not 2nd, so 3rd April here is correct.

The basic thing is that strtotime() just uses the same day-of-the-month when calculating in months -- so, on the 31st January, it reckons thus:

+1 month:  31st February -- *doesn't exist* converted to 3rd March
+2 months: 31st March    -- OK
+3 months: 31st April    -- *doesn't exist* converted to 1st May
+4 months: 31st May      -- OK
+5 months: 31st June     -- *doesn't exist* converted to 1st July

etc., etc.

So you just have to be careful when calculating next/previous months to allow for strangeness like this near the end of a month -- just as you have to watch out for DST transitions if calculating with times near 01:00/02:00/03:00.  If all you're interested in is the actual month, your best bet is to normalize to a date that exists in every month (such as 1st) before doing your next/previous calculation.

HTH

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: m.ford@xxxxxxxxxxxxxx
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 


To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux