Welcome to WebmasterWorld Guest from 54.204.100.232

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

End of Next Month Date

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

5+ 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

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

WebmasterWorld Senior Member 5+ Year Member



Looks like it works - substitute 12 for date("m").
7:20 pm on Feb 26, 2008 (gmt 0)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 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>';
?>