2

Provided the code stated below, the output are (see below). My question is why after 2009/11/01 it follow's by 2009/11/30 and 2009/12/30 instead of 2009/12/01. From 2009/06/01 ~ 2009/11/01 there is no problem.

output
2009/06/01
2009/07/01
2009/08/01
2009/09/01
2009/10/01
2009/11/01
2009/11/30
2009/12/30

my code

<?php

$startdate = "2009/06/01";
$enddate = "2009/12/31";

$start = strtotime($startdate); 
$end = strtotime($enddate); 

$currentdate = $start; 
while($currentdate < $end)
{
    $cur_date = date('Y/m/d',$currentdate);
    $month = date('m', $currentdate); 
    $year = date('Y', $currentdate); 
    $monthLength = daysOfMonth($month, $year); 
    $currentdate += $monthLength; 

    echo $cur_date . "<br />";  
}


function daysOfMonth($month, $year)
{
    return (86400 * date("t", strtotime($year."-".$month."-01")));
} 

?>
1
  • is Alvaro's answer adequate? You should either mark it as Accepted or comment on what's wrong still. Commented Dec 26, 2009 at 4:37

1 Answer 1

1
<?php

$startdate = "2009/06/01";
$enddate = "2009/12/31";

$start = strtotime($startdate);
$end = strtotime($enddate);

$currentdate = $start;
while($currentdate < $end)
{
        $cur_date = date('Y/m/d', $currentdate);

        $currentdate = strtotime('+1 month', $currentdate);

        echo $cur_date . "<br />";
}

?>
Sign up to request clarification or add additional context in comments.

2 Comments

hi alvaro, sorry for my late replied, due to Christmas holiday I wasn't able to work and read your answer. BTW, your code works perfectly, how about if the start date not started with day 01, let say $startdate = "2009/06/06" ? Thank you very much
Is it a new question? Once you have a Unix timestamp, you have a great choice of functions to manipulate it. You can extract fragments with date() and generate new timestamps with mktime().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.