Page 1 of 1

I hate working with dates - easy, but i can't figure it out

Posted: Fri Mar 19, 2004 5:12 am
by JayBird
I need to get the last day of the previous months e.g 30 or 31

i now how to get the last day of the current month like this...

Code: Select all

$lastday = mktime(0, 0, 0, $search_month-1, 0, $nowArray['year']);
so, i though i would be able to minus 1 from the $search_month, like this ...

Code: Select all

$lastday = mktime(0, 0, 0, ($search_month-1), 0, $nowArray['year']);
...but, if the current month in January (month 1) and you minus 1, the month now becomes 0, and the above code doesn't return anything!?

Help

Mark

Posted: Fri Mar 19, 2004 5:50 am
by patrikG
That's because you're substracting from the value of $search_month. If that is 1 and you substract 1 it becomes 0. Just add a

Code: Select all

$lastday = mktime(0, 0, 0, ((!$search_month-1) ? $search_month=12 : $search_month), 0, $nowArray['year']); ;

Re: I hate working with dates - easy, but i can't figure it

Posted: Fri Mar 19, 2004 8:06 am
by TheBentinel.com
Bech100 wrote:I need to get the last day of the previous months e.g 30 or 31

Code: Select all

$lastday = mktime(0, 0, 0, $search_month-1, 0, $nowArray['year']);
How about mktime(0,0,0, $search_month, 0, $nowArray['year'])?

The "0" day of a month is the last day of the previous month. So to get the last day of February, get the 0th of March.

Does that do what you want?

Posted: Fri Mar 19, 2004 9:06 am
by Weirdan
or just:

Code: Select all

echo date('Y m d',strtotime(date('M').' last day'));