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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

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

Post 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
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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']); ;
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

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

Post 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?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

or just:

Code: Select all

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