Jump to content

[SOLVED] How to get the previous months last date....


iPixel

Recommended Posts

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

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
*/

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.