iPixel Posted October 20, 2009 Share Posted October 20, 2009 Here's my wittle problem. I suck at working with dates. So today is 10/20/2009, i need to figure out how to use $lastmonth = date("m-d-Y", strtotime(" ")); in order to get 09/30/2009 Thanks! Link to comment https://forums.phpfreaks.com/topic/178347-solved-how-to-get-the-previous-months-last-date/ Share on other sites More sharing options...
ILMV Posted October 20, 2009 Share Posted October 20, 2009 Right, I haven't tested this, so you might need to play around with it a bit: date(’Y-m-d’,strtotime(’-1 second’,strtotime(date(’m').’/01/’.date(’Y').’ 00:00:00′))); Good luck Link to comment https://forums.phpfreaks.com/topic/178347-solved-how-to-get-the-previous-months-last-date/#findComment-940407 Share on other sites More sharing options...
iPixel Posted October 20, 2009 Author Share Posted October 20, 2009 what's this part for ? "#8242;)));" it seems to be breaking the code? Right, I haven't tested this, so you might need to play around with it a bit: date(Y-m-d,strtotime(-1 second,strtotime(date(m')./01/.date(Y'). 00:00:00′))); Good luck Link to comment https://forums.phpfreaks.com/topic/178347-solved-how-to-get-the-previous-months-last-date/#findComment-940410 Share on other sites More sharing options...
salathe Posted October 20, 2009 Share Posted October 20, 2009 It might make more sense to play with mktime in this particular case as I don't think there is a simple way to get the last day of the previous month with strtotime. An example using mktime is: // Show the last day of last month $last = mktime(0, 0, 0, date('n'), 0); echo "Last day of last month is: " . date("D, j M Y", $last) . "\n"; // Last day of last month is: Wed, 30 Sep 2009 Just to run that through some quick tests: // Some tests, one for each month of this year $months = explode("|", "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec"); foreach ($months as $month) { $day = rand(1, 28); $today = strtotime("$day $month 2009"); echo "\nFor " . date("d M y", $today) . ", LDOLM is "; echo date("d M y", mktime(0, 0, 0, date('n', $today), 0)); } /* Test dates are random but should be something like the following: For 22 Jan 09, LDOLM is 31 Dec 08 For 10 Feb 09, LDOLM is 31 Jan 09 For 27 Mar 09, LDOLM is 28 Feb 09 For 16 Apr 09, LDOLM is 31 Mar 09 For 14 May 09, LDOLM is 30 Apr 09 For 13 Jun 09, LDOLM is 31 May 09 For 09 Jul 09, LDOLM is 30 Jun 09 For 15 Aug 09, LDOLM is 31 Jul 09 For 22 Sep 09, LDOLM is 31 Aug 09 For 25 Oct 09, LDOLM is 30 Sep 09 For 15 Nov 09, LDOLM is 31 Oct 09 For 17 Dec 09, LDOLM is 30 Nov 09 */ Link to comment https://forums.phpfreaks.com/topic/178347-solved-how-to-get-the-previous-months-last-date/#findComment-940434 Share on other sites More sharing options...
ILMV Posted October 20, 2009 Share Posted October 20, 2009 Oops, that was a copy and paste screw-up. Link to comment https://forums.phpfreaks.com/topic/178347-solved-how-to-get-the-previous-months-last-date/#findComment-940442 Share on other sites More sharing options...
iPixel Posted October 20, 2009 Author Share Posted October 20, 2009 Worked perfectly, mktime will do just fine. Thank You! Link to comment https://forums.phpfreaks.com/topic/178347-solved-how-to-get-the-previous-months-last-date/#findComment-940444 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.