Jump to content

List all months since YYYY-MM-DD


Canman2005

Recommended Posts

Hi all

 

How can I print in a list format, all the dates since 2006-12-01 up to today, for for example

 

December 2006

Janurary 2007

February 2007

March 2007

April 2007

 

and then as we get into a new month, it would be added onto the bottom.

 

I'm sure ive asked this before but I cant seem to find the post or example I saved.

 

Any help would be spot on

 

Thanks

 

Dave

Link to comment
https://forums.phpfreaks.com/topic/50337-list-all-months-since-yyyy-mm-dd/
Share on other sites

<?php

$startdate = "1 december 2006";

function printMonths($var)
{
$start = strtotime($var);                     //timestamp of the entered date
$now = strtotime("Now");                    //timestamp of now so it does not write anything in the future
while ($start < $now)                         //while the start is less than now
{
echo date("F Y", $start);                    //echo the month and year
echo "<BR>";                                    //line break
$start = strtotime("+1 month", $start); // add a month onto the start timestamp
}
}
printMonths($startdate);	            //execute the function
?>

 

prints out

 

December 2006

January 2007

February 2007

March 2007

April 2007

May 2007

<?php

$startdate = "1 december 2006";

function printMonths($var)
{
$start = strtotime($var);
$now = strtotime("Now");
while ($start < $now)
{
	$dateArray[] = $start; //add the timestamps to an array
	$start = strtotime("+1 month", $start);
}
$revArray = array_reverse($dateArray); //reverse the array as they will be increasing in number
for($i=0; $i <count($revArray); $i++) //iterate over the array
{
	echo date("F Y", $revArray[$i]); //print out the dates
	echo "<BR>";	
}
}
printMonths($startdate);	//execute the function

?>

or making it even easier

<?php

$startdate = "1 december 2006";

function printMonths($var)
{
$start = strtotime($var);
$now = strtotime("Now");
while ($now > $start)
{
	echo date("F Y", $now);
	echo "<BR>";
	$now = strtotime("-1 month", $now);
}	
}

printMonths($startdate);	//execute the function

?>

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.