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

Link to comment
Share on other sites

<?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

?>

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.