Jump to content

month difference


jeeva

Recommended Posts

hi frnds

 

how can i get month difference between two months for example if i have selected 12-2006 and

05-2007 then i need to get  the dates like 12-2006,01-2007,02-2007,03-2007,04-2007 and 05-2007.

 

can i get like that?

 

 

--jeeva

 

 

 

Link to comment
Share on other sites

well, I would solve this problem by using timestamps. for expample:

$start=strtotime("2006-12-01");
$end=strtotime("2007-05-01");
for($x=$start;$x<$end){
$x+=86400*32; //No month has more than 31 days, so you're now in the next month
$x=mktime(0,0,0,date("m",$x),1,date("Y",$x)); //now the timestamps points to the first day of the month
echo date("m-Y");
}


Link to comment
Share on other sites

Here's a solution that just uses the strtotime() funciton:

<?php
$test=strtotime("2006-12-01");
$end=strtotime("2007-05-01");
$tmp = array();
while ($test <= $end) {
$tmp[] = date('m-Y',$test);
$test = strtotime("+1 month",$test);
}
echo implode(', ',$tmp);
?>

 

Ken

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.