phpzend-frameworkzend-date

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


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 

Solution

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