Pages

Get starting & Ending Date of the week

date_default_timezone_set('Asia/Calcutta'); //Set apprpriate timezone

$week_days = get_month_weekdays();

 $first_day_date = $week_days['start'];
 $end_day_date = $week_days['end'];
function get_month_weekdays() {
  $wk_num =  get_current_week_number(time());
  $first = 1;  
  $yr = date('Y', time());
  $wk_ts  = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
  $mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
  $week_days['start'] = date('d-m-Y', $mon_ts);
  $week_days['end']  = date('d-m-Y', strtotime('+6 days', strtotime($week_days['start'])));
  return $week_days;
}

function check_is_leap_year($year) {
  if ((($year % 4) == 0 && ($year % 100)!=0) || ($year % 400)==0) {
    return 1;
  } else {
    return 0;
  }
}
function check_iso_week_days($yday, $wday) {
  return $yday - (($yday - $wday + 382) % 7) + 3;
}
function get_current_week_number($timestamp) {
   $d = GETDATE($timestamp);
   $days = check_iso_week_days($d[ "yday"], $d[ "wday"]);
   if ($days < 0) {
     $d[ "yday"] += 365 + check_is_leap_year(--$d[ "year"]);
     $days = check_iso_week_days($d[ "yday"], $d[ "wday"]);
   }
   else {
     $d[ "yday"] -= 365 + check_is_leap_year($d[ "year"]);
     $d2 = check_iso_week_days($d[ "yday"], $d[ "wday"]);
     if (0 <= $d2) {
       $days = $d2;
     }
   }
   return (int)($days / 7) +1;
}

echo $first_day_date." to ".$end_day_date;

No comments:

Post a Comment