Como encontrar as datas entre duas datas especificadas?

Se eu tiver duas datas 20-4-2010 e 22-4-2010 em duas checkboxs de texto e eu quero que as datas sejam assim 20, 21, 22. Como faço isso?

Tenho certeza de que isso foi respondido um quadrillion vezes antes, mas, de qualquer maneira:

$start = strtotime('20-04-2010 10:00'); $end = strtotime('22-04-2010 10:00'); for($current = $start; $current <= $end; $current += 86400) { echo date('dm-Y', $current); } 

A parte 10:00 é evitar que o código salte ou repita um dia devido ao horário de verão.

Ao dar o número de dias:

 for($i = 0; $i <= 2; $i++) { echo date('dm-Y', strtotime("20-04-2010 +$i days")); } 

Com PHP5.3

 $period = new DatePeriod( new DateTime('20-04-2010'), DateInterval::createFromDateString('+1 day'), new DateTime('23-04-2010') // or pass in just the no of days: 2 ); foreach ( $period as $dt ) { echo $dt->format( 'dmY' ); } 

Você pode usar mktime() .

mktime () é útil para realizar a aritmética e a validação da data, já que calculará automaticamente o valor correto para a input fora de alcance.

Se você incrementar o número do dia, você receberá uma data válida, mesmo que você vá após o final do mês:

 '; } while ($dateCur < $dateEnd); 

Saída:

 25-04-10 26-04-10 27-04-10 28-04-10 29-04-10 30-04-10 01-05-10 02-05-10 03-05-10 

Você pode fazer:

 $start = strtotime("2010-04-20"); // get timestamp for start date. $end = strtotime("2010-04-22"); // get timestamp for end date. // go from start timestamp to end timestamp adding # of sec in a day. for($t=$start;$t<=$end;$t+=86400) { // get the date for this timestamp. $d = getdate($t); // print the date. echo $d['mday'].'-'.$d['mon'].'-'.$d['year']."\n"; } 

Saída:

 20-4-2010 21-4-2010 22-4-2010 
 /** * Function to list of weeks start and end. * @param string strDateFrom (Date From "YYYY-MM-DD") * @param string strDateTo (Date To "YYYY-MM-DD") * @return array weeks */ function getWeeksBetweenTowDates($date_from, $date_to) { $startweek = $current_week = date("W", strtotime($date_from)); $endweek = date("W", strtotime($date_to)); $current_year = date("Y", strtotime($date_from)); $current_yearweek = date("Y", strtotime($date_from)) . $startweek; $end_yearweek = date("Y", strtotime($date_to)) . $endweek; $start_day = 0; $end_day = 0; while ($current_yearweek <= $end_yearweek) { $dto = new DateTime(); if ($start_day == 0) { $start_day = $dto->setISODate(date("Y", strtotime($date_from)), $current_week, 0)->format('Ym-d'); $end_day = $dto->setISODate(date("Y", strtotime($date_from)), $current_week, 6)->format('Ym-d'); } else { $start_day = $dto->setISODate(date("Y", strtotime($end_day)), $current_week, 0)->format('Ym-d'); $end_day = $dto->setISODate(date("Y", strtotime($end_day)), $current_week, 6)->format('Ym-d'); } $arr_weeks[sprintf("%02d", $current_week)] = $start_day . " =>" . $end_day; $last_yearweek_in_year = $current_year . date("W", strtotime('31-12-' . $current_year)); if ($current_yearweek == $last_yearweek_in_year) { //last week in the year $current_week = 1; $current_year = ($current_year + 1); } else { $current_week = ($current_week + 1); } $current_yearweek = $current_year . sprintf("%02d", $current_week); } return $arr_weeks; } 

Executar como: date_from = 2015-10-20, date_to = 2016-04-15

 $arr_weeks = $this->getWeeksBetweenTowDates($date_from, $date_to); 

Saída:

  Array ( [43] => 2015-10-18 =>2015-10-24 [44] => 2015-10-25 =>2015-10-31 [45] => 2015-11-01 =>2015-11-07 [46] => 2015-11-08 =>2015-11-14 [47] => 2015-11-15 =>2015-11-21 [48] => 2015-11-22 =>2015-11-28 [49] => 2015-11-29 =>2015-12-05 [50] => 2015-12-06 =>2015-12-12 [51] => 2015-12-13 =>2015-12-19 [52] => 2015-12-20 =>2015-12-26 [53] => 2015-12-27 =>2016-01-02 [01] => 2016-01-03 =>2016-01-09 [02] => 2016-01-10 =>2016-01-16 [03] => 2016-01-17 =>2016-01-23 [04] => 2016-01-24 =>2016-01-30 [05] => 2016-01-31 =>2016-02-06 [06] => 2016-02-07 =>2016-02-13 [07] => 2016-02-14 =>2016-02-20 [08] => 2016-02-21 =>2016-02-27 [09] => 2016-02-28 =>2016-03-05 [10] => 2016-03-06 =>2016-03-12 [11] => 2016-03-13 =>2016-03-19 [12] => 2016-03-20 =>2016-03-26 [13] => 2016-03-27 =>2016-04-02 [14] => 2016-04-03 =>2016-04-09 [15] => 2016-04-10 =>2016-04-16 ) 

Tente isso, espero que ajude a ajuda

 $begin = date("Ymd", strtotime($date); $end = date("Ymd", strtotime($date)); $begin = new DateTime($begin); $end = new DateTime($end); for ($i = $begin; $i <= $end; $i=$i->modify('+1 day')) { echo $i->format('Ym-d'); }