Re: Dynamic Date List Newbie Problem

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

 



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


[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