从当前日期开始的日期数组

时间:2018-12-20 20:08:34

标签: php laravel date php-carbon

美好的一天。

我一直在尝试使用Laravel中的Carbon来获取当前日期。因此,如果今天是12月20日,我想获得一个数组:

December 20, 2018
December 21, 2018
December 22, 2018
December 23, 2018
December 24, 2018

我想做2周,甚至3周。这就是我到目前为止所得到的。

$currentdate = Carbon::now()->format('m/d/Y');
$ts = strtotime($currentdate);

$year = date('o', $ts);
$week = date('W', $ts);

$datearray = [];
for($i = 1; $i <= 7; $i++) {
   $ts = strtotime($year.'W'.$week.$i);
   array_push($datearray,date("m/d/Y", $ts));
}

上面的代码从一周的开始给我一周的时间,而不是从一天以后的一天开始。

2 个答案:

答案 0 :(得分:0)

尝试一下,

$datearray = [];
for($i = 0; $i < 7; $i++) {
   array_push($datearray, date('F d, Y', strtotime($i == 0 ?'today UTC':'today +'.$i.'day')));
   //push to array as 'December 20, 2018'
}

享受编码〜! :)

答案 1 :(得分:0)

如果您已经在使用Carbon,请使用它的强大功能!

use \Carbon\Carbon;

// how many days you want in your array
$count = 7;
$dates = [];
$date = Carbon::now();
for ($i = 0; $i < $count; $i++) {
    $dates[] = $date->addDay()->format('F d, Y');
}

// Show me what you got
print_r($dates);

输出为:

Array
(
    [0] => December 22, 2018
    [1] => December 23, 2018
    [2] => December 24, 2018
    [3] => December 25, 2018
    [4] => December 26, 2018
    [5] => December 27, 2018
    [6] => December 28, 2018
)