Aswin Lutchanah

Hybrid Social Media Specialist + Php/MySql Developer Metz (Développeur Php/MySql Metz) + SEO-SMO

Php function to show the week date range for any month

How to show the the week date range in for any month ?
Like: 3november-9november ,10nov-16nov so on…

Following a question of Rajiv Kumar on Facebook, I’ve developed a first version of the php function below to get the first and last day of each week of a month. There are four parameters:

  1. $iYear is the variable for the year
  2. $iMonth is the numerical representation of the month (1 – 12)
  3. $sFirstDayOfWeek takes either Monday or Sunday as the first day of the week; default is Monday.
  4. $bExclusive is a boolean which when true takes only weeks within the chosen month but when false, it allows overlapping to take into account all weeks that are related to the chosen month.

DEMO

  1. function getFullWeeksOfMonth($iYear, $iMonth, $sFirstDayOfWeek=‘Monday’, $bExclusive = true) {
  2.     $iYear = filter_var($iYear, FILTER_VALIDATE_INT, array(
  3.         ‘options’ => array(
  4.             ‘default’ => (int) date(‘Y’)
  5.         )
  6.     ));
  7.     $iMonth = filter_var($iMonth, FILTER_VALIDATE_REGEXP, array(
  8.         ‘options’ => array(
  9.             ‘default’ => (int) date(‘m’),
  10.             ‘regexp’ => ‘/^([1-9]|1[012])$/’
  11.         )
  12.     ));
  13.     $aDay = array(‘monday’=>1, ‘sunday’=>1);
  14.     $sFirstDayOfWeek = filter_var($sFirstDayOfWeek , FILTER_VALIDATE_REGEXP, array(
  15.         ‘options’ => array(
  16.             ‘default’ => ‘monday’,
  17.             ‘regexp’ => ‘/^monday|sunday$/’
  18.         )
  19.     ));
  20.     $bExclusive =  filter_var($bExclusive,FILTER_VALIDATE_BOOLEAN);
  21.     $oStart = new DateTime($iYear . ‘-‘ . $iMonth . ‘-01’);
  22.  
  23.     if ($bExclusive === true || ($bExclusive === false && isset($aDay[strtolower($oStart->format(‘l’))]))) {
  24.         if ((int) $oStart->format(‘d’) === 1) {
  25.             $oStart->modify(‘-1 day’);
  26.         }
  27.         $oStart->modify(‘first ‘ . $sFirstDayOfWeek . ‘ ‘ . $oStart->format(‘H:i’));
  28.     } else {
  29.         $oStart->modify(‘last ‘ . $sFirstDayOfWeek . ‘ ‘ . $oStart->format(‘H:i’));
  30.     }
  31.  
  32.     $oEnd = clone($oStart);
  33.     if((int)$oStart->format(‘m’)===$iMonth){
  34.         $oEnd->modify(‘last day of this month’);
  35.     }else{
  36.         $oEnd->modify(‘last day of next month’);
  37.     }
  38.  
  39.     $oInterval = new DateInterval(‘P1W7D’);
  40.     $oDaterange = new DatePeriod($oStart, $oInterval, $oEnd);
  41.  
  42.     $aDate = array();
  43.     $i = 1;
  44.     foreach ($oDaterange as $oDate) {
  45.         $oTestDate = clone $oDate;
  46.         $oLastWeekDay = $oTestDate->modify(‘+6 days’);
  47.         if (
  48.                 ((int) $oDate->format(‘m’) === (int) $iMonth || (int) $oLastWeekDay->format(‘m’) === (int) $iMonth) &&
  49.                 (($bExclusive === true && (int) $oLastWeekDay->format(‘m’) === (int) $iMonth) ||
  50.                 ($bExclusive === false))
  51.         ) {
  52.             $aDate[$i][‘First’] = $oDate->format(‘Y-m-d’);
  53.             $aDate[$i][‘Last’] = $oLastWeekDay->format(‘Y-m-d’);
  54.         }
  55.         $i++;
  56.     }
  57.     return $aDate;
  58. }
  59. print_r(getFullWeeksOfMonth(2014, 10, ‘Monday’));

Posted In: PHP, Web Development

Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*