-2

Below is my script which works fine for all starting dates except 29,30 and 31. Can any one make it work for all dates starting from 1....31st it should increment by month and donot exceed last date ?

  $startdate='2010-01-30';
  $enddate='2011-01-30';
    while ($startdate <= $enddate)
     { 
      echo date('Y-m-d', $startdate ) . "\n";
      $startdate = strtotime('+1 month', $startdate);
    }
Asad Kayani
  • 31
  • 1
  • 4
  • 1
    It's obvious because Feburary doesn't have 31st date. – Yogesh Suthar Sep 17 '13 at 06:40
  • @Yogesh Of course it's obvious. Give him the fix instaed of downvoting. – Jan Doggen Sep 17 '13 at 06:42
  • 1
    @JanDoggen: It's hard to fix when what he wants is not provided. We could guess that he wants the last day of each month, but it's not certain that that's what he would want. – Steven Liao Sep 17 '13 at 06:44
  • @JanDoggen I haven't downvoted him :) Maybe someone else has done that. – Yogesh Suthar Sep 17 '13 at 06:50
  • Sorry guys I was assuming to much. @Asad What do you want to accomplish? – Jan Doggen Sep 17 '13 at 06:51
  • @JanDoggen And the fix may be that OP has to use `$startdate='2010-01-28'; $enddate='2011-01-28';`. – Yogesh Suthar Sep 17 '13 at 06:59
  • check my answer below for solution. It just increments timestamp by one day every time to take the timestamp into next month and then use 'Y-m-t' in date function to extract the last day of that month – rakeshjain Sep 17 '13 at 07:04
  • @all i want to increment date e.g($startdate="2010-01-29" and $enddate="2010-12-29" ) increment it by month it sholud give 2010-02-29, 2010-02-28 , 2010-03-29 ....... so on till 2010-12-29 . kindly help me with this any one – Asad Kayani Sep 17 '13 at 10:43
  • @AsadKayani you want same day of month for each of the months that fall between start date and end date. Its just that if the month of the day is not valid for a particular month, you want last day of that month. Am I right? – rakeshjain Sep 17 '13 at 18:13
  • @rakeshjain yes exactly same thing i want – Asad Kayani Sep 18 '13 at 05:35

3 Answers3

5

Try this

<?php
  $startdate='2010-01-31';
  $enddate='2011-01-31';
    $timestamp=  strtotime($startdate);
    while ($startdate <= $enddate)
     { 
      $startdate = date('Y-m-t', $timestamp);
      echo $startdate . "<br/>";
      $timestamp = strtotime('+1 days', strtotime($startdate));
    }
?>
rakeshjain
  • 1,791
  • 11
  • 11
0

Try

<?php
    $startdate = strtotime('2010-01-29');
    $enddate = strtotime('2011-01-31');
$inc = 1;
    while($startdate <= $enddate )
    {
        $date = date("t", $startdate);
        $numbersofdays = date('d',$startdate);
        $incDate = ($date - $numbersofdays) + 1;
        if ($startdate == $enddate || $inc==1) {
            echo date('Y-m-d',$startdate).  PHP_EOL;
        } else {
        echo date('Y-m-t',$startdate).  PHP_EOL;
       }

         $startdate = strtotime("+".$incDate." days", $startdate );
        $inc++;
    }
?>
ratnesh dwivedi
  • 352
  • 2
  • 8
0

This looks like complicated code, but I believe this is what you required.

<?php
$startDate='2010-01-29';
$endDate='2013-12-30';
$startDateSplit = explode("-",$startDate);
$endDateSplit = explode("-",$endDate);
$starTime = mktime(0, 0, 0, $startDateSplit[1], $startDateSplit[2], $startDateSplit[0]);
$numDaysStart = $startDateSplit[2];
$passThroughDelta = false;
$endTime = mktime(0, 0, 0, $endDateSplit[1], $endDateSplit[2], $endDateSplit[0]);
while ($starTime <= $endTime)
{
      $startDate = date('Y-m-d', $starTime);
      echo $startDate . "\n";
      $startDateSplit = explode("-",$startDate);
      $numDays = cal_days_in_month(CAL_GREGORIAN, $startDateSplit[1],$startDateSplit[0]);
      echo $numDays . "\n";
      if($startDateSplit[1] == 12) {
         $startDateSplit[1] = 0;
         $startDateSplit[0]++;
      }
      if($numDaysStart > cal_days_in_month(CAL_GREGORIAN,     $startDateSplit[1]+1,$startDateSplit[0])) {
       $delta = $numDaysStart - cal_days_in_month(CAL_GREGORIAN,     $startDateSplit[1]+1,$startDateSplit[0]);
           $numDays = $numDays - $delta;
           $passThroughDelta = true;
      }
      else if ($passThroughDelta) {
           $numDays = $numDays + $delta;
           $passThroughDelta = false;
      }
      $starTime = mktime(0, 0, 0, $startDateSplit[1], $startDateSplit[2]+$numDays,     $startDateSplit[0]);
}
Vineesh
  • 467
  • 4
  • 11
  • your code gives wrong output if $startDate='2010-01-30'; $endDate='2013-12-30'; kindly help me with the script which runs correct with date 28,29,30 and 31. – Asad Kayani Sep 17 '13 at 10:20
  • Hi Asad, In this code I am trying to add the number of days in the month next to the startDate, see this line, $numDays = cal_days_in_month(CAL_GREGORIAN, $startDateSplit[1]+1,$startDateSplit[0]); ... I have tried with the date provided by you in this script. I got the output as 2010-01-30 28 2010-02-27 31 2010-03-30 so on. can you tell me where is the error? So that I can try more. – Vineesh Sep 17 '13 at 12:47
  • i want to give any date between 1,2,3,.....31st of month and it should run for next dates also like if i pass $startdate="2010-02-28" it should output 2010-01-28 2010-02-28 2010-03-28 uptil last date , and for case like $startdate="2010-01-29" expected output 2010-01-29 2010-02-28 2010-03-29 so on... – Asad Kayani Sep 18 '13 at 05:02
  • I have edited my code to adjust as per your requirement, Please see it work for you. I have tested the code with the following date, $startdate="2010-02-28" , $startdate="2010-02-29" .. And it works fine for me. – Vineesh Sep 18 '13 at 08:04