Get last day in previous months date();

Hi,

I’m using this to get last day in previous months:

$ldmo = date("Y-m-t", mktime(0, 0, 0, date("m")-5  , date("d"), date("Y")));

It seems to work, until it comes to February where it just misses that month completely by jumping from 2009-03-31 to 2009-01-31, that’s when I have date(“m”)-11 and date(“m”)-12.

So how can I do it so it can wrk with every month.

Thanks!

That’s because there is no 31st in those months, and today date(‘d’) is 31.

To get the last day of a month, ask for the 0th day of the following month.

Here we go - an alternative approach complete with source code:

Beware:
To find the last day of the previous month the offfset used is only one second If and only if Summer-Time is used then it would be beneficial to set the offset at one hour and one second (1+606060).

Online demo with source code:
http://johns-jokes.com/downloads/sp/first-day-of-month/

Screen dump:
http://www.graabr.com/l1cZS6/

The source code:

<?php
	$title = 'http://www.sitepoint.com/forums/showthread.php?t=658330';
	define('jj','<br />');
	define('jt','-');
	date_default_timezone_set('UTC');
	
	echo '<fieldset>';
		echo "<legend><a href='$title'>$title</a></legend>";
		echo jj;
		highlight_file('index.php');
	echo '</fieldset>';
	

	$style='font-size:1.4em; line-height:1.6em; padding-left:4em';
	echo "<p style='$style'>";
		echo jj, 'Date format: Y-M-D ==> ';
		echo $y = date("Y");
		echo jt, $m = date("m");
		echo jt, $d = date("d");
		
		echo jj, jj, '<b>First day of month &nbsp;&nbsp;&nbsp;&nbsp;one second before</b>';
		for($i2=12; $i2>0; $i2--)
		{
			$firstday 	= mktime(0, 0, 0, $m-$i2, 1, date("Y"));
			echo jj, date("Y-m-d - M", $firstday), '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			echo date(" M Y-m-d", $firstday-1); 
		}	
	echo '</p>';	
	
// best not to terminate with ?>





.

Sure there’s no shorter way of doing this like one code liner or something.

This code does it with one line.
Other lines are just for decoration.

But as Dan told you before

To get the last day of a month, ask for the 0th day of the following month.

The idea of the lengthy code that I showed and also the screen dumps was to show that the code caters for all dates. The code can be be cut, pasted and modified to learn what is happening.

The relevant lines are below and the basic idea is to subtract one second from the very first day of the month.



 $firstday     = mktime(0, 0, 0, date("m"), 1, date("Y")); 
 echo date(" M Y-m-d", $firstday-1); 


.

Thanks will try that.

You can cut those two lines down to one by using:

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

Many thanks I am pleased to say it works and today I learnt that midnight was yesterday :slight_smile:

Screen dump:
http://www.graabr.com/l13I4R/

The online solution:
http://johns-jokes.com/downloads/sp/first-day-of-month/

Midnight is today, the trick is to ask for midnight of day zero of the month. There is no such thing as “January 0, 2010”, but the underlying code implementing mktime() interprets it specially to mean you want the last day of the previous month.

This is what I now have:-

Today: date(“Y-m-d”);

Yesterday: date(“Y-m-d”, mktime(0, 0, 0, date(“m”) , date(“d”)-1, date(“Y”)));

Last 7 Days: date(“Y-m-d”, mktime(0, 0, 0, date(“m”) , date(“d”)-7, date(“Y”)));

Last 30 Days: date(“Y-m-d”, mktime(0, 0, 0, date(“m”) , date(“d”)-30, date(“Y”)));

First Day Of Current Month:
$yd = date(“Y”);
$md = date(“m”);
$tmo = “$yd-$md-01”;

First Day Of Last Month:
date(“Y-m-d”, mktime(0, 0, 0, date(“m”)-1, +1, date(“Y”)));

Last Day Of Last Month:
$ldlmo = date(“Y-m-t”, mktime(0, 0, 0, date(“m”), 0, date(“Y”)));

Does this look fine, wrked it all out myself apart from what you helped me on from the above posts. Anything looking wrong, please let me know. Hope it’s all right.

Yesterday: date(“Y-m-d”, strtotime(“yesterday”));
7 days ago: date(“Y-m-d”, strtotime(“-7 days”));
30 days ago: date(“Y-m-d”, strtotime(“-30 days”));

That’s shorter, thanks!

Last day of previous month:


echo date('t', strtotime('last month'));

PHP 5.3


echo date('F jS, Y', strtotime('last day of last month'));
#January 31st, 2010


<?php
$d = new DateTime( '2010-02-05' );
$d->modify( 'last day of last month' );
echo $d->format( 'F jS, Y' ), "\
";
#January 31st, 2010
?>