All the week numbers between two dates in PHP
All the week numbers between two dates in PHP
getNoOfWeek() will return array of week no. with year. You can change formatting of this array from week_text_alter().
function getNoOfWeek($startDate, $endDate){ // convert date in valid format $startDate = date("Y-m-d", strtotime($startDate)); $endDate = date("Y-m-d", strtotime($endDate)); $yearEndDay = 31; $weekArr = array(); $startYear = date("Y", strtotime($startDate)); $endYear = date("Y", strtotime($endDate)); if($startYear != $endYear) { $newStartDate = $startDate; for($i = $startYear; $i <= $endYear; $i++) { if($endYear == $i) { $newEndDate = $endDate; } else { $newEndDate = $i."-12-".$yearEndDay; } $startWeek = date("W", strtotime($newStartDate)); $endWeek = date("W", strtotime($newEndDate)); if($endWeek == 1){ $endWeek = date("W", strtotime($i."-12-".($yearEndDay-7))); } $tempWeekArr = range($startWeek, $endWeek); array_walk($tempWeekArr, "week_text_alter", array('pre' => 'Week ', 'post' => " '". substr($i, 2, 2) )); $weekArr = array_merge($weekArr, $tempWeekArr); $newStartDate = date("Y-m-d", strtotime($newEndDate . "+1 days")); } } else { $startWeek = date("W", strtotime($startDate)); $endWeek = date("W", strtotime($endDate)); $endWeekMonth = date("m", strtotime($endDate)); if($endWeek == 1 && $endWeekMonth == 12){ $endWeek = date("W", strtotime($endYear."-12-".($yearEndDay-7))); } $weekArr = range($startWeek, $endWeek); array_walk($weekArr, "week_text_alter", array('pre' => 'Week ', 'post' => " '". substr($startYear, 2, 2))); } $weekArr = array_fill_keys($weekArr, 0); return $weekArr; } function week_text_alter(&$item1, $key, $prefix) { $item1 = $prefix['pre']. $item1 . $prefix['post']; } Output 1 - $weekArr = getNoOfWeek('2014-01-01', '2014-02-10'); Array ( [Week 1 '14] => 0 [Week 2 '14] => 0 [Week 3 '14] => 0 [Week 4 '14] => 0 [Week 5 '14] => 0 [Week 6 '14] => 0 [Week 7 '14] => 0 ) Output 2 - $weekArr = getNoOfWeek('2013-12-01', '2014-02-10'); Array ( [Week 48 '13] => 0 [Week 49 '13] => 0 [Week 50 '13] => 0 [Week 51 '13] => 0 [Week 52 '13] => 0 [Week 1 '14] => 0 [Week 2 '14] => 0 [Week 3 '14] => 0 [Week 4 '14] => 0 [Week 5 '14] => 0 [Week 6 '14] => 0 [Week 7 '14] => 0 )
Tags: Week no between date range, Week count in 2 date range, Display week no between two dates, Array of all weeks between 2 dates
Created At: 05 April, 2014
Views: 10,256
Social Sharing
Search
Articles
Recently Added Jobs
Rajasthan High Court - Jodhpur Recruitment 2017 for Civil Judges
Last Date: 21 December, 2017
Last Date: 21 December, 2017
WBSEDCL Recruitment - 2017 for Office Executive
Last Date: 16 May, 2017
Last Date: 16 May, 2017
RECRUITMENT OF PROBATIONARY OFFICERS IN STATE BANK OF INDIA
Last Date: 06 March, 2017
Last Date: 06 March, 2017
UPSC Combined Medical Services Examination 2015
Last Date: 10 April, 2015
Last Date: 10 April, 2015
UPSC Engineering Services Examination 2015
Last Date: 10 April, 2015
Last Date: 10 April, 2015