RE: [PHP] strange behaviour with strtotime()
| From: | Ford, Mike | Date: | Tue, 31 Jan 2006 17:09:20 +0000 |
| Subject: | RE: [PHP] strange behaviour with strtotime() | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-229571@lists.php.net to get a copy of this message | ||
On 31 January 2006 15:41, Barry wrote:
> Ford, Mike wrote:
> > Yes, but 1 apple is 1 apple is 1 apple.
> >
> > 1 month can be 28 days, or 31 days, or anything in between.
> 2 months can be anything from 59 to 62 days.
> >
> > A month is an imprecise measure, and adding them together
> will get you an even more imprecise result.
> >
> > Deal with it -- that's just the way it is.
> >
> > Cheers!
>
> And now you tell me how the strtotime function tells what to use
> either 28 days or 31 days or 4 3/87 days.
It's very simple. It uses the same day-of-the-month you start from: 1st gets you the 1st; 15th
gets you the 15th; 31st gets you 31st or equivalent). Put another way, "+1 month" adds as
many days as there are days in the starting month; "+2 months" adds as many days as there
are in the starting month and the one after (starting in Jan: 31+28 = 59 -- but starting in, say,
Jul: 31+31 = 62); and so on.
Most of the time this gives you exactly what you want -- it's only when you ask starting from
the 29th, 30th or 31st of a month, and the destination month is shorter, that it causes any kind of
problem. Since this is a very small, well-defined range of dates, it's fairly simple to deal
with it -- once you know you need to!!
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@leedsmet.ac.uk
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