-1

Is there a way to put the last 40 sundays into an array with php? I don't get it right now.

tried the following

$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);
}
ismaelw
  • 341
  • 2
  • 17
  • Yes. Show us what you've tried first and then we'll help you out. – John Conde Sep 25 '13 at 12:53
  • What have you tried? Per the flagging menu: _"Questions asking for code ***must demonstrate a minimal understanding of the problem being solved***. Include attempted solutions, why they didn't work, and the expected results. See also: [Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)."_ – War10ck Sep 25 '13 at 12:58
  • Related question is here already : http://stackoverflow.com/questions/336127/calculate-business-days – Jenson M John Sep 25 '13 at 12:58

3 Answers3

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

See it in action

John Conde
  • 217,595
  • 99
  • 455
  • 496
1

should work:

<?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);
?>

see example

Adam
  • 1,371
  • 2
  • 11
  • 12
0

Yes, of course. Use this code

$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;  
}
zavg
  • 10,351
  • 4
  • 44
  • 67