Cecil
Cecil

Reputation: 1619

echo each date between a given value in PHP

I have a form that allows users to enter a date when they are available in the format dd/mm/yyyy.

What I want to be able to do is echo out each date between now (including today) and the date entered.

Could someone help me out? I'm not quite sure how to go about it.

For example, if someone was to enter 12/12/2011 and the date today was 10/12/2011 it would echo the following:

10/12/2011  
11/12/2011  
12/12/2011

Any help would be great.

Upvotes: 0

Views: 711

Answers (4)

amitchd
amitchd

Reputation: 975

First of all you are using a DD-MM-YYYY, make sure your separators are - so that you don't have converting them to timestamps. Here is an example with the current format etc.

You should have more checks to make sure you do not get an infinite loop :).

$start_date = "10/12/2011";
$end_date = "12/12/2011";
print_r(dates_between($start_date, $end_date));

function dates_between($start_date, $end_date)
{
    $date = array($start_date);
    $start_date = str_replace('/', '-', $start_date);
    $end_date = str_replace('/', '-', $end_date);

    $current_date = $start_date;
    while($current_date != $end_date)
    {
        $current_date = date("d-m-Y", strtotime("+1 day", strtotime($current_date)));
        $date[] = str_replace('-', '/', $current_date);
    }
    return $date;
}

Upvotes: 0

bensiu
bensiu

Reputation: 25564

$start_date = time ();  // today
$end_date = strtotime ( .. your input date as string ... );
while ( $start_date + 86400 < $end_date ) {
  echo date ('m/d/Y', $start_date );
  $start_date += 86400; // full day
}

Upvotes: 4

kylex
kylex

Reputation: 14406

//Assume dates are properly formatted.
$startDate;
$endDate;

while($startDate < $endDate){  
     $startDate = date("m/d/Y", strtotime("+1 day", strtotime($startDate))); 
     echo $startDate;

}

Upvotes: 0

ayush
ayush

Reputation: 14568

picked from here

<?php
$fromDate = ’01/01/2009′;
$toDate = ’01/10/2009′;

$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);

for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr = date(“Y-m-d”,$currentDateTS);
$dateMonthYearArr[] = $currentDateStr;
//print $currentDateStr.”<br />”;
}

echo  “<pre>”;
print_r($dateMonthYearArr);
echo “</pre>”;
?>

Upvotes: 0

Related Questions