-1

有没有办法用 php 将最后 40 个星期日放入一个数组中?我现在不明白。

尝试了以下

$week_array = array();
$last_s = date('Y-m-d',strtotime('last sunday'));
array_push($week_array, $last_s);       
for ($i = 0; $i <= 40; $i++ ) {
  $last_s = $last_s - 7;
  array_push($week_array, $last_s);
}
4

3 回答 3

1

应该管用:

<?php
for ($i = 0; $i < 40 ; $i++){
   $week = 3600*24*7; 
   $dates[] = date("Y-m-d",strtotime(date("Y-m-d",strtotime("last Sunday")))-$week*$i) ;

}
print_r($dates);
?>

看例子

于 2013-09-25T13:00:31.560 回答
1
<?php
$sundays = array();
$now = new DateTime();
if ($now->format('l') === 'Sunday') {
    $sundays[] = $now->format("Y-m-d");
}
$dt = new DateTime('last sunday');
while (count($sundays) < 40) {
    $sundays[] = $dt->format("Y-m-d");
    $dt->modify('-1 week');
}
print_r($sundays);

看到它在行动

于 2013-09-25T12:58:20.723 回答
0

是的当然。使用此代码

$sundays = array();

// $prev is auxillary variable which holds the time 
// from which we are searching the next last Sunday
$prev = time();

for($i = 0; $i < 40; $i++)
{
  $prev = strtotime('last Sunday', $prev);  // 
  $sundays[] = $prev;  
}
于 2013-09-25T12:56:23.833 回答