bsamson Posted March 31, 2008 Share Posted March 31, 2008 I have the following script: <?php // Get last 3 months for search criteria $longMonth1 = date("M Y"); $longMonth2 = date("M Y",strtotime("-1 month")); echo $longMonth1."<br>".$longMonth2; ?> This was working fine the other day, however today (03/31/2008) it is displaying: Mar 2008 Mar 2008 It should display: Mar 2008 Feb 2008 Any suggestions will be greatly appreciated! Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 31, 2008 Share Posted March 31, 2008 Because 02/31/2008 does not exist (today - 1 month) you get the next valid date (the start of this month.) The php manual recommends forming a date in the current month that would always exist (they used the 15th, but anything from the 1st to the 28th would work) and then subtract one month. You might also want to consider using the mktime() function - echo date("M Y",mktime(0, 0, 0, date("m"), 1, date("Y"))); echo date("M Y",mktime(0, 0, 0, date("m")-1, 1, date("Y"))); Quote Link to comment Share on other sites More sharing options...
bsamson Posted March 31, 2008 Author Share Posted March 31, 2008 Thanks for the clarification! It's working great! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.