RE: [PHP] Date addition problem
Date of today +1 .. +x days http://www.weberdev.com/get_example-292.html Sincerely berber Visit the Weber Sites Today, To see where PHP might take you tomorrow. SEO Data Monitor http://seo.weberdev.com -Original Message- From: Adrian Bruce [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 29, 2006 2:52 PM To: php-general@lists.php.net Subject: [PHP] Date addition problem Hi 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] date test"; $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"$i: $startdate -- -- --$nextdate"; } ?> OUTPUT: 0: 07-09-05 -- -- --07-09-05 1: 07-09-05 -- -- --14-09-05 2: 07-09-05 -- -- --21-09-05 3: 07-09-05 -- -- --28-09-05 4: 07-09-05 -- -- --05-10-05 5: 07-09-05 -- -- --12-10-05 6: 07-09-05 -- -- --19-10-05 7: 07-09-05 -- -- --26-10-05 8: 07-09-05 -- -- --01-11-05 9: 07-09-05 -- -- --08-11-05 [/snip] Thanks a lot Ade -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date addition problem
Hi: The problem is that you have transposed your date elements. See this: http://xn--ovg.com/a3.php Try this code: date test (d/m/y)"; $start = "05-09-07"; $start = explode('-',$start); $startmk = mktime(0,0,0,$start[1],$start[0],$start[2]); $startdate = date('d-m-y',$startmk); print_r($start); for($i=0;$i<10;$i++){ $nextdate = date('d-m-y',$startmk + ($i*604800)); echo"$i: $startdate -- -- --$nextdate"; } ?> Best, tedd --- At 1:51 PM +0100 3/29/06, Adrian Bruce wrote: Hi 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] date test"; $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"$i: $startdate -- -- --$nextdate"; } ?> OUTPUT: 0: 07-09-05 -- -- --07-09-05 1: 07-09-05 -- -- --14-09-05 2: 07-09-05 -- -- --21-09-05 3: 07-09-05 -- -- --28-09-05 4: 07-09-05 -- -- --05-10-05 5: 07-09-05 -- -- --12-10-05 6: 07-09-05 -- -- --19-10-05 7: 07-09-05 -- -- --26-10-05 8: 07-09-05 -- -- --01-11-05 9: 07-09-05 -- -- --08-11-05 [/snip] Thanks a lot Ade -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Date addition problem
> -Original Message- > From: Adrian Bruce [mailto:[EMAIL PROTECTED] > 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] > > echo" date test"; > $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"$i: $startdate -- -- --$nextdate"; > } > ?> 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 4730Fax: +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
Re: [PHP] Date addition problem
I have come up with an alternative myself but still cant explain why the first method did not work, still be interested to know why if anyone can offer some thoughts. Using strtotime for the calc seems to produce correct results my soluiton: [snip] date test 2"; $start = "05-09-07"; $start = explode('-',$start); $startmk = mktime(0,0,0,$start[1],$start[2],$start[0]); $startdate = date('Y-m-d',$startmk); for($i=0;$i<10;$i++){ $add = $i*7; $nextdate = date('d-m-y', strtotime($startdate." + ".$add." days")); echo"$i: $startdate -- -- --$nextdate"; } ?> [/snip] Adrian Bruce wrote: That should not effect it, if i am adding $i * 7 days each time then i should get the date that comes 7 days later irrespective of how many days are in the month, shouldn't I ? 8 * 7 = 56, 56 days after the 07-09-05 is 02-11-2005. Thomas Munz wrote: Cause 9th month have 30 days but 10th month have 31 days. You don't check the amount of day there... :) on Wednesday 29 March 2006 14:51, Adrian Bruce wrote: Hi 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] date test"; $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"$i: $startdate -- -- --$nextdate"; } ?> OUTPUT: 0: 07-09-05 -- -- --07-09-05 1: 07-09-05 -- -- --14-09-05 2: 07-09-05 -- -- --21-09-05 3: 07-09-05 -- -- --28-09-05 4: 07-09-05 -- -- --05-10-05 5: 07-09-05 -- -- --12-10-05 6: 07-09-05 -- -- --19-10-05 7: 07-09-05 -- -- --26-10-05 8: 07-09-05 -- -- --01-11-05 9: 07-09-05 -- -- --08-11-05 [/snip] Thanks a lot Ade -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date addition problem
That should not effect it, if i am adding $i * 7 days each time then i should get the date that comes 7 days later irrespective of how many days are in the month, shouldn't I ? 8 * 7 = 56, 56 days after the 07-09-05 is 02-11-2005. Thomas Munz wrote: Cause 9th month have 30 days but 10th month have 31 days. You don't check the amount of day there... :) on Wednesday 29 March 2006 14:51, Adrian Bruce wrote: Hi 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] date test"; $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"$i: $startdate -- -- --$nextdate"; } ?> OUTPUT: 0: 07-09-05 -- -- --07-09-05 1: 07-09-05 -- -- --14-09-05 2: 07-09-05 -- -- --21-09-05 3: 07-09-05 -- -- --28-09-05 4: 07-09-05 -- -- --05-10-05 5: 07-09-05 -- -- --12-10-05 6: 07-09-05 -- -- --19-10-05 7: 07-09-05 -- -- --26-10-05 8: 07-09-05 -- -- --01-11-05 9: 07-09-05 -- -- --08-11-05 [/snip] Thanks a lot Ade -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date addition problem
Cause 9th month have 30 days but 10th month have 31 days. You don't check the amount of day there... :) on Wednesday 29 March 2006 14:51, Adrian Bruce wrote: > Hi > > 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] > > echo" date test"; > $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"$i: $startdate -- -- --$nextdate"; > } > ?> > > OUTPUT: > > 0: 07-09-05 -- -- --07-09-05 > 1: 07-09-05 -- -- --14-09-05 > 2: 07-09-05 -- -- --21-09-05 > 3: 07-09-05 -- -- --28-09-05 > 4: 07-09-05 -- -- --05-10-05 > 5: 07-09-05 -- -- --12-10-05 > 6: 07-09-05 -- -- --19-10-05 > 7: 07-09-05 -- -- --26-10-05 > 8: 07-09-05 -- -- --01-11-05 > 9: 07-09-05 -- -- --08-11-05 > > [/snip] > > Thanks a lot > > Ade -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php