Re: Dynamic Date List Newbie Problem

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

 



$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 <Cool@xxxxxxxxxxxxxxxx>
To: <php-general@xxxxxxxxxxxxx>
Date: Thu, 12 Mar 2009 09:24:48 -0700
Subject:  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


[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