RE: strange behaviour with strtotime()

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

 



On 31 January 2006 13:50, James Benson wrote:

> Barry 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 ^^
> > 
> > So its not logic.
> > month +2 should output 2nd april!
> > 
> > Barry
> 
> 
> 
> ok i understand now, that is fairly f****d up if you ask me, the
> documentation should state this side affect, one month should be one
> month from that date not +31 days, if i wated +31 days i
> would ask for it :)

Well, it's not always 31 days from now -- that depends on the month!  For strtotime(), +1 month is always the same day-of-the-month in the next month -- so in some months that's 31 days away, others it's 30 days away, and in February it's 28 or 29 days away.

The problem comes when you ask for "+1 month" (or "next month") from the 31st of a long month -- do you mean "the last day of next month" or do you mean "the equivalent of 31st of next month"?  strtotime() takes the latter approach, but of course that's not always what you want.  In those cases, you just have to beware and check your answer -- if the month component ends up 2 months away, instead of 1, then you need to adjust your answer back the appropriate number of days.  Personally, I find mktime() more suited to this task than strtotime() as you're working with specific day/month/year numbers, but use whatever you find most comfortable.

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