[PHP] Dynamic Date List Newbie Problem
Hi folks, My goal was to create a dynamic date related list as follows: - take the current date (3/12/2009) - create a new date from it which would be the *1st* of that month - (3/1/2009) - then create a list that shows the current and previous 11 months like: 03/1/09 02/1/09 01/1/09 12/1/08 11/1/08 etc... --- so I did this (not fancy but was working) $nowts = strtotime(date(m)./1/.date(y)); $m01 = date('m',$nowts-(0))./1/.date('y',$nowts-(0)); $m02 = date('m',$nowts-(86400*30))./1/.date('y',$nowts-(86400*30)); $m03 = date('m',$nowts-(86400*60))./1/.date('y',$nowts-(86400*60)); $m04 = date('m',$nowts-(86400*90))./1/.date('y',$nowts-(86400*90)); $m05 = date('m',$nowts-(86400*120))./1/.date('y',$nowts-(86400*120)); $m06 = date('m',$nowts-(86400*150))./1/.date('y',$nowts-(86400*150)); $m07 = date('m',$nowts-(86400*180))./1/.date('y',$nowts-(86400*180)); $m08 = date('m',$nowts-(86400*210))./1/.date('y',$nowts-(86400*210)); $m09 = date('m',$nowts-(86400*240))./1/.date('y',$nowts-(86400*240)); $m10 = date('m',$nowts-(86400*270))./1/.date('y',$nowts-(86400*270)); $m11 = date('m',$nowts-(86400*300))./1/.date('y',$nowts-(86400*300)); $m12 = date('m',$nowts-(86400*330))./1/.date('y',$nowts-(86400*330)); PROBLEM: all was fine for the last few months but several days back the 1st few months were wrong - like this... 03/1/09 02/1/09 12/1/08 * wrong 12/1/08 11/1/08 etc... * I think the math went wrong because FEB had 28 days - not 30 (not sure why just 1 month JANUARY was wrong...) I temporarily fixed it with less than 30 day jumps like: $m01 = date('m',$nowts-(0))./1/.date('y',$nowts-(0)); $m02 = date('m',$nowts-(86400*28))./1/.date('y',$nowts-(86400*28)); $m03 = date('m',$nowts-(86400*58))./1/.date('y',$nowts-(86400*58)); $m04 = date('m',$nowts-(86400*88))./1/.date('y',$nowts-(86400*88)); $m05 = date('m',$nowts-(86400*118))./1/.date('y',$nowts-(86400*118)); $m06 = date('m',$nowts-(86400*150))./1/.date('y',$nowts-(86400*150)); $m07 = date('m',$nowts-(86400*180))./1/.date('y',$nowts-(86400*180)); Q: Any ideas how to fix this issue? (please try to keep it simple - 'cause I ain't no math wiz either) -- Thanks - RevDave Cool @ hosting4days . com [db-lists 09] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamic Date List Newbie Problem
$currmonth = date(m); for ($i = 1; $i = 11; $i++) { $m[$i] = date(m/d/y), mktime(0,0,0,$currmonth-$i, 1, 2009)); } Something like that. mktime() is remarkably flexible. If you start on month 3 and you subtract 5, you get month = -2. So that would be -2/1/2009, which mktime will translate to 11/1/2008. This is totally off the top of my head, but you get the idea. Works with days and years too. Sometimes it's useful when going backwards and not sure how many days the previous month had (without doing another call to find out). -TG - Original Message - From: revDAVE c...@hosting4days.com To: php-general@lists.php.net Date: Thu, 12 Mar 2009 09:24:48 -0700 Subject: [PHP] Dynamic Date List Newbie Problem Hi folks, My goal was to create a dynamic date related list as follows: - take the current date (3/12/2009) - create a new date from it which would be the *1st* of that month - (3/1/2009) - then create a list that shows the current and previous 11 months like: 03/1/09 02/1/09 01/1/09 12/1/08 11/1/08 etc... --- so I did this (not fancy but was working) $nowts = strtotime(date(m)./1/.date(y)); $m01 = date('m',$nowts-(0))./1/.date('y',$nowts-(0)); $m02 = date('m',$nowts-(86400*30))./1/.date('y',$nowts-(86400*30)); $m03 = date('m',$nowts-(86400*60))./1/.date('y',$nowts-(86400*60)); $m04 = date('m',$nowts-(86400*90))./1/.date('y',$nowts-(86400*90)); $m05 = date('m',$nowts-(86400*120))./1/.date('y',$nowts-(86400*120)); $m06 = date('m',$nowts-(86400*150))./1/.date('y',$nowts-(86400*150)); $m07 = date('m',$nowts-(86400*180))./1/.date('y',$nowts-(86400*180)); $m08 = date('m',$nowts-(86400*210))./1/.date('y',$nowts-(86400*210)); $m09 = date('m',$nowts-(86400*240))./1/.date('y',$nowts-(86400*240)); $m10 = date('m',$nowts-(86400*270))./1/.date('y',$nowts-(86400*270)); $m11 = date('m',$nowts-(86400*300))./1/.date('y',$nowts-(86400*300)); $m12 = date('m',$nowts-(86400*330))./1/.date('y',$nowts-(86400*330)); PROBLEM: all was fine for the last few months but several days back the 1st few months were wrong - like this... 03/1/09 02/1/09 12/1/08 * wrong 12/1/08 11/1/08 etc... * I think the math went wrong because FEB had 28 days - not 30 (not sure why just 1 month JANUARY was wrong...) I temporarily fixed it with less than 30 day jumps like: $m01 = date('m',$nowts-(0))./1/.date('y',$nowts-(0)); $m02 = date('m',$nowts-(86400*28))./1/.date('y',$nowts-(86400*28)); $m03 = date('m',$nowts-(86400*58))./1/.date('y',$nowts-(86400*58)); $m04 = date('m',$nowts-(86400*88))./1/.date('y',$nowts-(86400*88)); $m05 = date('m',$nowts-(86400*118))./1/.date('y',$nowts-(86400*118)); $m06 = date('m',$nowts-(86400*150))./1/.date('y',$nowts-(86400*150)); $m07 = date('m',$nowts-(86400*180))./1/.date('y',$nowts-(86400*180)); Q: Any ideas how to fix this issue? (please try to keep it simple - 'cause I ain't no math wiz either) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamic Date List Newbie Problem
On Thu, Mar 12, 2009 at 12:24 PM, revDAVE c...@hosting4days.com wrote: Hi folks, My goal was to create a dynamic date related list as follows: - take the current date (3/12/2009) - create a new date from it which would be the *1st* of that month - (3/1/2009) - then create a list that shows the current and previous 11 months like: 03/1/09 02/1/09 01/1/09 12/1/08 11/1/08 etc... --- so I did this (not fancy but was working) $nowts = strtotime(date(m)./1/.date(y)); $m01 = date('m',$nowts-(0))./1/.date('y',$nowts-(0)); $m02 = date('m',$nowts-(86400*30))./1/.date('y',$nowts-(86400*30)); $m03 = date('m',$nowts-(86400*60))./1/.date('y',$nowts-(86400*60)); $m04 = date('m',$nowts-(86400*90))./1/.date('y',$nowts-(86400*90)); $m05 = date('m',$nowts-(86400*120))./1/.date('y',$nowts-(86400*120)); $m06 = date('m',$nowts-(86400*150))./1/.date('y',$nowts-(86400*150)); $m07 = date('m',$nowts-(86400*180))./1/.date('y',$nowts-(86400*180)); $m08 = date('m',$nowts-(86400*210))./1/.date('y',$nowts-(86400*210)); $m09 = date('m',$nowts-(86400*240))./1/.date('y',$nowts-(86400*240)); $m10 = date('m',$nowts-(86400*270))./1/.date('y',$nowts-(86400*270)); $m11 = date('m',$nowts-(86400*300))./1/.date('y',$nowts-(86400*300)); $m12 = date('m',$nowts-(86400*330))./1/.date('y',$nowts-(86400*330)); PROBLEM: all was fine for the last few months but several days back the 1st few months were wrong - like this... 03/1/09 02/1/09 12/1/08 * wrong 12/1/08 11/1/08 etc... * I think the math went wrong because FEB had 28 days - not 30 (not sure why just 1 month JANUARY was wrong...) I temporarily fixed it with less than 30 day jumps like: $m01 = date('m',$nowts-(0))./1/.date('y',$nowts-(0)); $m02 = date('m',$nowts-(86400*28))./1/.date('y',$nowts-(86400*28)); $m03 = date('m',$nowts-(86400*58))./1/.date('y',$nowts-(86400*58)); $m04 = date('m',$nowts-(86400*88))./1/.date('y',$nowts-(86400*88)); $m05 = date('m',$nowts-(86400*118))./1/.date('y',$nowts-(86400*118)); $m06 = date('m',$nowts-(86400*150))./1/.date('y',$nowts-(86400*150)); $m07 = date('m',$nowts-(86400*180))./1/.date('y',$nowts-(86400*180)); Q: Any ideas how to fix this issue? (please try to keep it simple - 'cause I ain't no math wiz either) -- Thanks - RevDave Cool @ hosting4days . com [db-lists 09] Unless you are in a timezone that does not observe DST, you can't use a constant 86400 as the number of seconds in a day. You could try something like this: ?php $date = strtotime('-' . (date('d') - 1) . ' days'); for ($i = -11; $i = 0; ++$i) { $list[] = date('m/d/Y', strtotime($i months, $date)); } var_dump($list); ? Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamic Date List Newbie Problem
Crap, I hit the wrong button and sent this only to the OP... On Thu, Mar 12, 2009 at 09:24:48AM -0700, revDAVE wrote: Hi folks, My goal was to create a dynamic date related list as follows: - take the current date (3/12/2009) - create a new date from it which would be the *1st* of that month - (3/1/2009) - then create a list that shows the current and previous 11 months like: 03/1/09 02/1/09 01/1/09 12/1/08 11/1/08 etc... --- so I did this (not fancy but was working) $nowts = strtotime(date(m)./1/.date(y)); $m01 = date('m',$nowts-(0))./1/.date('y',$nowts-(0)); $m02 = date('m',$nowts-(86400*30))./1/.date('y',$nowts-(86400*30)); $m03 = date('m',$nowts-(86400*60))./1/.date('y',$nowts-(86400*60)); $m04 = date('m',$nowts-(86400*90))./1/.date('y',$nowts-(86400*90)); $m05 = date('m',$nowts-(86400*120))./1/.date('y',$nowts-(86400*120)); $m06 = date('m',$nowts-(86400*150))./1/.date('y',$nowts-(86400*150)); $m07 = date('m',$nowts-(86400*180))./1/.date('y',$nowts-(86400*180)); $m08 = date('m',$nowts-(86400*210))./1/.date('y',$nowts-(86400*210)); $m09 = date('m',$nowts-(86400*240))./1/.date('y',$nowts-(86400*240)); $m10 = date('m',$nowts-(86400*270))./1/.date('y',$nowts-(86400*270)); $m11 = date('m',$nowts-(86400*300))./1/.date('y',$nowts-(86400*300)); $m12 = date('m',$nowts-(86400*330))./1/.date('y',$nowts-(86400*330)); PROBLEM: all was fine for the last few months but several days back the 1st few months were wrong - like this... 03/1/09 02/1/09 12/1/08 * wrong 12/1/08 11/1/08 etc... * I think the math went wrong because FEB had 28 days - not 30 (not sure why just 1 month JANUARY was wrong...) I temporarily fixed it with less than 30 day jumps like: $m01 = date('m',$nowts-(0))./1/.date('y',$nowts-(0)); $m02 = date('m',$nowts-(86400*28))./1/.date('y',$nowts-(86400*28)); $m03 = date('m',$nowts-(86400*58))./1/.date('y',$nowts-(86400*58)); $m04 = date('m',$nowts-(86400*88))./1/.date('y',$nowts-(86400*88)); $m05 = date('m',$nowts-(86400*118))./1/.date('y',$nowts-(86400*118)); $m06 = date('m',$nowts-(86400*150))./1/.date('y',$nowts-(86400*150)); $m07 = date('m',$nowts-(86400*180))./1/.date('y',$nowts-(86400*180)); Q: Any ideas how to fix this issue? (please try to keep it simple - 'cause I ain't no math wiz either) Here is working code to do it better: // get your starting date $darray = getdate(); $month = $darray['mon']; $year = $darray['year']; // create your months $dt = array(); for ($i = $month; $i = 1; $i--) { $dt[] = date('m/d/y', mktime(0, 0, 0, $i, 1, $year)); } $year--; for ($i = 12; $i $month; $i--) { $dt[] = date('m/d/y', mktime(0, 0, 0, $i, 1, $year)); } // print the dates, just as a check for ($i = 0; $i count($dt); $i++) { print $dt[$i] . br\n; } This gives you a decending array from the current 1st of the month to twelve months prior. The $dt array has mm/dd/yy date strings in it. Change as needed. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamic Date List Newbie Problem
On 3/12/2009 9:39 AM, TG tg-...@gryffyndevelopment.com wrote: $currmonth = date(m); for ($i = 1; $i = 11; $i++) { $m[$i] = date(m/d/y), mktime(0,0,0,$currmonth-$i, 1, 2009)); } Something like that. mktime() is remarkably flexible. If you start on month 3 and you subtract 5, you get month = -2. So that would be -2/1/2009, which mktime will translate to 11/1/2008. This is totally off the top of my head, but you get the idea. Works with days and years too. Sometimes it's useful when going backwards and not sure how many days the previous month had (without doing another call to find out). -TG SOLVED - Thanks so much Andrew TG From your examples - I was able to create this working table going back 2 years - thanks again! table border=1 cellspacing=2 cellpadding=2 tr tdDates/td /tr ?php $currmonth = date(m); $curryear = date(y); $i = 0; do { ? tr td?php $m[$i] = date(m/d/y, mktime(0,0,0,$currmonth-$i, 1, $curryear)); echo $m[$i].'br /'; $i++ ? /td /tr ?php } while ($i 25); ? /table -- Thanks - RevDave Cool @ hosting4days . com [db-lists 09] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamic Date List Newbie Problem
On 3/12/2009 12:25 PM, Paul M Foster pa...@quillandmouse.com wrote: Crap, I hit the wrong button and sent this only to the OP... On Thu, Mar 12, 2009 at 09:24:48AM -0700, revDAVE wrote: Thanks for your help Paul - that makes sense! Here is working code to do it better: // get your starting date $darray = getdate(); $month = $darray['mon']; $year = $darray['year']; // create your months $dt = array(); for ($i = $month; $i = 1; $i--) { $dt[] = date('m/d/y', mktime(0, 0, 0, $i, 1, $year)); } $year--; for ($i = 12; $i $month; $i--) { $dt[] = date('m/d/y', mktime(0, 0, 0, $i, 1, $year)); } // print the dates, just as a check for ($i = 0; $i count($dt); $i++) { print $dt[$i] . br\n; } This gives you a decending array from the current 1st of the month to twelve months prior. The $dt array has mm/dd/yy date strings in it. Change as needed. Paul -- Thanks - RevDave Cool @ hosting4days . com [db-lists 09] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php