Calculate all dates between 2 dates in php? [duplicate]

醉酒当歌 提交于 2019-12-23 02:32:35

问题


I have 2 dates

$st_date = '2012-07-20';
$ed_date = '2012-07-27';

I want to display all dates from $st_date to $ed_date As:

2012-07-20
2012-07-21
2012-07-22
2012-07-23
2012-07-24
2012-07-25
2012-07-26
2012-07-27

I thought first count difference, run foreach < count, and add $i day to $st_date.
But i don't want loop, it increases code. Any of direct date() which return an array of all-dates.


回答1:


Without loop using range() & array_map() :

EDIT: a little mistake, you have to jump 86400, because 1 day = 86400 seconds, so the code should be fine now :)

    $st_date = '2012-07-20';
    $ed_date = '2012-07-27';
    $dates = range(strtotime($st_date), strtotime($ed_date),86400);
    $range_of_dates = array_map("toDate", $dates);
    print_r($range_of_dates);
    function toDate($x){return date('Y-m-d', $x);}

?>



回答2:


try this code :

<?php
$st_date = '2012-07-20';
$ed_date = '2012-07-27';

$dateMonthYearArr = array();
$st_dateTS = strtotime($st_date);
$ed_dateTS = strtotime($ed_date);

for ($currentDateTS = $st_dateTS; $currentDateTS <= $ed_dateTS; $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>”;
?>



回答3:


Whilst I don't understand your aversion to loops, I do understand hiding code away as much as possible.

To that end, I would extend PHP's DateTime object to give the functionality you are after. Something like this:-

class MyDateTime extends DateTime
{
    /**
    * Creates an array of date strings of all days between
    * the current date object and $endDate
    * 
    * @param DateTime $endDate
    * @return array of date strings
    */
    public function rangeOfDates(DateTime $endDate)
    {
        $result = array();
        $interval = new DateInterval('P1D');
        //Add a day as iterating over the DatePeriod
        //misses the last day for some strange reason
        //See here http://www.php.net/manual/en/class.dateperiod.php#102629
        $endDate->add($interval); 
        $period = new DatePeriod($this, $interval, $endDate);
        foreach($period as $day){
            $result[] = $day->format('Y-m-j');
        }
        return $result;
    }
}

Then when you want to use it, you can do this:-

$st_date = new MyDateTime("2012-07-20");
$en_date = new DateTime("2012-07-27");
$dates = $st_date->rangeOfDates($en_date);
var_dump($dates);

Will give the following output:-

array
  0 => string '2012-07-20' (length=10)
  1 => string '2012-07-21' (length=10)
  2 => string '2012-07-22' (length=10)
  3 => string '2012-07-23' (length=10)
  4 => string '2012-07-24' (length=10)
  5 => string '2012-07-25' (length=10)
  6 => string '2012-07-26' (length=10)
  7 => string '2012-07-27' (length=10)

Although, unfortunately, you will probably need a loop to iterate over that array :)

Obviously, this solution uses loops to achieve its goal, but they are encapsulated in a nice re-usable piece of code.

See the PHP manual on DateTime, DateInterval and DatePeriod for more information. There are lots of tips in the comments to those pages.




回答4:


$start = strtotime('2009-02-01'); 
$end = strtotime('2009-03-10'); 
$range = array();

$date = strtotime("-1 day", $start);  
while($date < $end)  { 
   $date = strtotime("+1 day", $date);
   $range[] = date('Y-m-d', $date);
} 



回答5:


$st_date = '2012-07-20';
$ed_date = '2012-07-27';

for($i=$st_date;$i<=$ed_date;$i++)echo $i."<br />";



回答6:


Little late to the party, but was tackling this issue earlier and did it with the following, which is cleaner than other examples here, so adding it as an option for people;

function dateRange($from, $to)
{
    return array_map(function($arg) {
         return date('Y-m-d', $arg);
    }, range(strtotime($from), strtotime($to), 86400));
}

Usage:

$dates = dateRange('2017-01-01', '2017-01-08');

Returns:

Array
(
    [0] => 2017-01-01
    [1] => 2017-01-02
    [2] => 2017-01-03
    [3] => 2017-01-04
    [4] => 2017-01-05
    [5] => 2017-01-06
    [6] => 2017-01-07
    [7] => 2017-01-08
)


来源:https://stackoverflow.com/questions/11556354/calculate-all-dates-between-2-dates-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!