RE: [PHP] Date addition problem
| From: | Ford, Mike | Date: | Wed, 29 Mar 2006 13:33:16 +0000 |
| Subject: | RE: [PHP] Date addition problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-232815@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Adrian Bruce [mailto:abruce@stvincent.ac.uk]
> Sent: 29 March 2006 13:52
>
> I am having an unusual problem when trying to calculate dates
> in advance
> from a start date. the code below shows a loop where by on
> each run an
> increasing number of weeks is added to the start date, this works as
> expected up untill the 8th time where for some reason it produces
> 01-11-05 instead of 02-11-05. I am at a loss as to why this would
> happen when it works perfectly for all the other dates. where am i
> going wrong?
>
> [snip]
>
> <?php
> echo"<h1> date test</h1>";
> $start = "05-09-07";
> $start = explode('-',$start);
> $startmk = mktime(0,0,0,$start[1],$start[2],$start[0]);
> $startdate = date('d-m-y',$startmk);
>
> for($i=0;$i<10;$i++){
> $nextdate = date('d-m-y',$startmk + ($i*604800));
> echo"<h1>$i: $startdate -- -- --$nextdate</h1>";
> }
> ?>
You're not allowing for DST shifts. 02-11-05 is after the clocks go back
an hour, so instead of getting 00:00 on 02-11-05 you're getting 11:00 on
01-11-05.
I always suggest working with the *other* 12 o'clock when calculating
purely date offsets -- 12:00 may still shift to 11:00 or 13:00, but this
will not affect the date!
Cheers!
Mike
------------------------------------------------------------------------
----------------
Mike Ford, Electronic Information Services Adviser, Learning Support
Services,
JG125, The Library, James Graham Building, Headingley Campus, Beckett
Park,
LEEDS, LS6 3QS, United Kingdom
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