-1

I am building an app on php,mysql which is related to events. This is a specific feature where the user will repeat an activity/event . The user will select a start date and an end date . Now i am trying to figure out how to calculate the dates between those two selected dates. I am using jquery's datepicker for date selection .

     $start_date = date("Y-m-d", strtotime($_POST['start_date']));
   $end_date = date("Y-m-d", strtotime($_POST['end_date']));

Datediff doesn't seem to do the work as it only gives the difference between two dates . I want all the dates between those two dates .

Would be great if someone could advice as per how to go about this .

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1411837
  • 1,494
  • 6
  • 27
  • 41
  • What do you want to do with those dates? – Indra Kumar S Jan 17 '15 at 15:15
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Jan 18 '15 at 01:32

3 Answers3

0

By this way You can fetch the dates between two dates

   $start_date = strtotime($_POST['start_date']);
    $end_date = strtotime($_POST['end_date']);

    // Loop between timestamps, 24 hours at a time
    for ( $i = $start_date ; $i <= $end_date ; $i = $i + 86400 ) {
              echo   date( 'Y-m-d', $i )."<br>";          

    }
Indra Kumar S
  • 2,818
  • 2
  • 16
  • 27
-1

I would recommend you to use DateTime objects

date_default_timezone_set("Europe/Istanbul");
$dt = new DateTime("10-01-2014");
$diff = $dt->diff(new Datetime("12-01-2014"));
for($i = 1; $i <= $diff->days; $i++) {
   $nextdate = $dt->modify("+1 day");
   echo $nextdate->format("d-m-Y"); echo "\n";
}
Can Tecim
  • 577
  • 8
  • 18
  • Did you test yourself before posting here???? – Indra Kumar S Jan 17 '15 at 15:26
  • This doesnt seem to work at all ....$dt = new DateTime($_POST['start_date']); and $diff = $dt->diff($_POST['end_date']); make no sense as they throw blank outputs... – user1411837 Jan 17 '15 at 15:30
  • They are sample code please read the DateTime [documentation](http://php.net/manual/en/class.datetime.php) I dont know what your start_date field contains so you may need to change little bit this sample code – Can Tecim Jan 17 '15 at 15:35
  • and also valid datetime formats for constructor of DateTime http://php.net/manual/en/datetime.formats.php – Can Tecim Jan 17 '15 at 15:36
  • I changed the code to correct one you can understand how it works now – Can Tecim Jan 17 '15 at 15:42
-1
function createDateRangeArray($strDateFrom,$strDateTo)
    {       
        $aryRange=array();

        $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
        $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

        if ($iDateTo>=$iDateFrom)
        {
            array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
            while ($iDateFrom<$iDateTo)
            {
                $iDateFrom+=86400; // add 24 hours
                array_push($aryRange,date('Y-m-d',$iDateFrom));
            }
        }
        return $aryRange;
    }

Found the above solution and it works great. Thanks everyone for the time and advice.

user1411837
  • 1,494
  • 6
  • 27
  • 41
  • I really recommend you to create your own function base on my comment. I edited the code you will understand now how it works. http://stackoverflow.com/questions/28000840/php-mysql-select-all-the-dates-between-two-chosen-dates/28000930#28000930 – Can Tecim Jan 17 '15 at 15:44