Forum Moderators: coopster

Message Too Old, No Replies

End of Next Month Date

         

itledi

4:18 pm on Feb 23, 2008 (gmt 0)

10+ Year Member



I'm trying to create a date for "the end of next month".

date("Y-m-d",mktime(0,0,0,date("m")+2,0));

So far I've come up with this. Will this output the zero hour of the next month? What happens if it were December 2008? Would it be the end of January 2009 or 2008?

Thanks

cameraman

7:29 pm on Feb 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks like it works - substitute 12 for date("m").

whoisgregg

7:20 pm on Feb 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would a strtotime [php.net] based solution. First build a timestamp of the first second of the current month, then use strtotime to advance two months, then use strtotime to move backward one second. The sample code below illustrates this approach.

<?php
$first_second_this_month = date('Y').'-'.date('m').'-01'.' 00:00:00';
echo '<p>$first_second_this_month: '.$first_second_this_month.'</p>';
$last_second_next_month = strtotime("-1 second", strtotime("+2 months", strtotime($first_second_this_month)));
echo '<p>$last_second_next_month: '.date("Y-m-d H:i:s", $last_second_next_month).'</p>';
?>