How print all the weeks in an year (or first monday of year)

岁酱吖の 提交于 2019-12-20 04:30:07

问题


How print all the weeks which start with monday and end with sunday.. like below ..using Zend_date

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

回答1:


Start by finding the first monday, then you can just add 1 week until the year increments.

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



回答2:


Getting first monday of the year:

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

}


来源:https://stackoverflow.com/questions/2099522/how-print-all-the-weeks-in-an-year-or-first-monday-of-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!