1

我有 2 个约会

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

我想显示从$st_date到的所有日期$ed_date

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

我想首先计算差异,运行 foreach < count,然后将 $i 天添加到 $st_date。
但我不想要循环,它会增加代码。返回所有日期数组的任何直接日期()。

4

6 回答 6

3

试试这个代码:

<?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.”&lt;br />”;
}

echo  “&lt;pre>”;
print_r($dateMonthYearArr);
echo “&lt;/pre>”;
?>
于 2012-07-19T08:11:36.450 回答
3

虽然我不理解您对循环的厌恶,但我确实理解尽可能地隐藏代码。

为此,我将扩展 PHP 的DateTime对象以提供您所追求的功能。像这样的东西: -

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;
    }
}

然后当你想使用它时,你可以这样做:-

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

将给出以下输出:-

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)

虽然,不幸的是,您可能需要一个循环来迭代该数组:)

显然,该解决方案使用循环来实现其目标,但它们被封装在一段可重用的代码中。

有关更多信息,请参阅有关DateTimeDateIntervalDatePeriod的 PHP 手册。这些页面的评论中有很多提示。

于 2012-07-19T09:20:42.623 回答
2
$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);
} 
于 2012-07-19T08:06:54.437 回答
2

不使用 range() 和 array_map() 循环:

编辑:一个小错误,你必须跳到 86400,因为 1 天 = 86400 秒,所以现在代码应该没问题了 :)

    $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);}

?>
于 2012-07-19T09:50:49.750 回答
1
$st_date = '2012-07-20';
$ed_date = '2012-07-27';

for($i=$st_date;$i<=$ed_date;$i++)echo $i."<br />";
于 2012-07-19T08:02:58.313 回答
0

聚会有点晚了,但早先解决了这个问题,并使用以下内容进行了处理,这比此处的其他示例更干净,因此将其添加为人们的选择;

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

用法:

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

回报:

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
)
于 2017-08-02T15:10:12.573 回答