1

如何打印从星期一开始到星期天结束的所有星期.. 如下所示..使用 Zend_date

1   04-Jan-2010-10-Jan-2010  
2   11-Jan-2010-17-Jan-2010 
3   18-Jan-2010-24-Jan-2010 
4

2 回答 2

8

从找到第一个星期一开始,然后您只需添加 1 周,直到年份递增。

<?php
define('NL', "\n");

$year           = 2010;
$firstDayOfYear = mktime(0, 0, 0, 1, 1, $year);
$nextMonday     = strtotime('monday', $firstDayOfYear);
$nextSunday     = strtotime('sunday', $nextMonday);

while (date('Y', $nextMonday) == $year) {
    echo date('c', $nextMonday), '-', date('c', $nextSunday), NL;

    $nextMonday = strtotime('+1 week', $nextMonday);
    $nextSunday = strtotime('+1 week', $nextSunday);
}
于 2010-01-20T08:01:26.107 回答
1

获得一年中的第一个星期一:

$year = 2010;

$date = new Zend_Date();
$date->set("01.01.$year", Zend_Date::DATES);

while(true)
{
   if($date->equals('Mon', Zend_Date::MONTH_NAME_SHORT))
   {
      //It's monday - print date
      break;
   }
   else
   {
       //It's not monday - move to the next day
       $date->add('1', Zend_Date::DAY_SHORT);
   }

}
于 2010-01-20T07:29:57.650 回答