1

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 
Charles
  • 50,943
  • 13
  • 104
  • 142
Nisanth Kumar
  • 5,667
  • 8
  • 33
  • 43

2 Answers2

8

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);
}
nikc.org
  • 16,462
  • 6
  • 50
  • 83
  • please note that the first Monday of the year is not not necessarily the same thing as the Monday of ISO week 1. Week 1 is deemed to be the week that holds the first Thurday of a year. so, for example week 1 of 2013 will begin on 31/12/2013. If you want to factor this into your calculation do something like this: $firstDayOfYear = mktime(0, 0, 0, 1, 1, $year); $first_thursday = strtotime('thursday', $firstDayOfYear); $first_monday = strtotime(date("Y-m-d",$first_thursday)." - 3 days"); – Kevin Bradshaw Aug 24 '11 at 21:31
  • @Kevin: Thanks, wasn't aware of that. You are indeed right: http://en.wikipedia.org/wiki/ISO_week_date#First_week But as I understand the OP's question, he's looking for full weeks during a year. – nikc.org Aug 25 '11 at 06:39
1

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

}
Community
  • 1
  • 1
Petr Peller
  • 8,581
  • 10
  • 49
  • 66