I have a PHP function to find the dates coming between two dates in 'd/m/Y' format.

function dateRange($first, $last, $step = '+1 day', $format = 'd/m/Y' ) 
{ 
        echo $first;
        echo "<br>";
        echo $last;
        echo "<br>";
        echo $step;
        echo "<br>";
        $dates = array();
        $current = strtotime($first);
        $last = strtotime($last);
        while( $current <= $last ) 
        { 
            $dates[] = date($format, $current);
            $current = strtotime($step, $current);
        }
        print_r($dates);
        exit;
        return $dates;
}   

Here

$first = 20/05/2014 

$last = 31/05/2014

As I have exited the function just after printing first result, It should print one date. But the date it is printing is 01/01/1970

What is wrong with this function ?

有帮助吗?

解决方案

just make some change in your function

$current = strtotime(str_replace("/", "-", $first));
$last = strtotime(str_replace("/", "-", $last));

instead of

 $current = strtotime($first);
 $last = strtotime($last);

because strtotime cannot covert the timestamp of the date format d/m/Y. it will convert the timestamp of the date format d-m-Y

其他提示

Here's how to do this using DateTime() and related classes:

function dateRange($first, $last, $step = '+1 day', $format = 'd/m/Y' ) 
{ 
    $dates    = array();
    $start    = DateTime::createFromFormat($format, $first);
    $finish   = DateTime::createFromFormat($format, $last);
    $finish->modify($step); // make sure last date is included
    $interval = DateInterval::createFromDateString($step);     
    $period   = new DatePeriod($start, $interval, $finish);    
    foreach ($period as $date) {
        $dates[] = $date->format($format);
    }
    return $dates;
}   

This solves the problem you are having with your date format which will not work due to how strtotime() handles dates with slashes. It assumes m/d/Y and not d/m/Y. It will also allow you to change the format into something not acceptable to strtotime() and still work.

try to replace '/' to '-'

$first = str_replace('/', '-', '20/05/2014');
$last = str_replace('/', '-', '31/05/2014');

so code will be:-

function dateRange($first, $last, $step = '+1 days', $format = 'd/m/Y' ) 
{ 
        echo $first;
        echo "<br>";
        echo $last;
        echo "<br>";
        echo $step;
        echo "<br>";
        $dates = array();
        $current = strtotime($first);
        $last = strtotime($last);
        while( $current <= $last ) 
        { 
            $dates[] = date($format, $current);
            $current = strtotime($step, $current);
        }
        print_r($dates);
        exit;
        return $dates;
}   
$first = str_replace('/', '-', '20/05/2014');
$last = str_replace('/', '-', '31/05/2014');
dateRange($first, $last, $step = '+1 day', $format = 'd/m/Y' ); 

output :-

20-05-2014
31-05-2014
+1 day
Array ( [0] => 20/05/2014 [1] => 21/05/2014 [2] => 22/05/2014 [3] => 23/05/2014 [4] => 24/05/2014 [5] => 25/05/2014 [6] => 26/05/2014 [7] => 27/05/2014 [8] => 28/05/2014 [9] => 29/05/2014 [10] => 30/05/2014 [11] => 31/05/2014 ) 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top