Courses Designed for Students / Working Professionals, We Teach Python, Java, RPA UI Path, Golang, Cordova, MySQL, SQL

Blog -> Bloglist

Calculating week of the year for given N week ago from Current week in PHP

Function details

This is function has written to get start week details and end week details for given (N) weeks ago
Note: Required PHP version 5.3 or greater
Current Date : 19-Feb-2014
Input : Start Date / End Date of Current week(201408), week with - (2014-08) current : 2014-02-16, 2014-02-22
Expected Output : Start Date / End Date of week(201403), week with - (2014-03) 5 ago: 2014-01-12, 2014-01-18
Result : Array( [currentweek] => 08 [currentdate] => 08, 2014-02-19, 3 [startdateofcurrentweek] => 2014-02-16 [enddateofcurrentweek] => 2014-02-22 [weekago] => 201403 [startdateofagoweek] => 2014-01-12 [enddateofagoweek] => 2014-01-18 )
$iWeeksAgo = 5;// need weeks ago -- INPUT $sWeekDayStartOn = 0;// 0 - Sunday, 1 - Monday, 2 - Tuesday $aWeeksDetails = getWeekDetails($iWeeksAgo, $sWeekDayStartOn); print_r($aWeeksDetails); die('end of line of getWeekDetails '); function getWeekDetails($iWeeksAgo, $sWeekDayStartOn){ $date = new DateTime(); $sCurrentDate = $date->format('W, Y-m-d, w'); $iWeekOfTheYear = $date->format('W');// Week of the Year i.e. 19-Feb-2014 = 08 // day of week for the current month i.e. 19-Feb-2014 = 4 $iDayOfWeek = $date->format('w'); $iDayOfMonth = $date->format('d'); // date of the month i.e. 19-Feb-2014 = 19 // number of days adding to get last date of the week //i.e. 19-Feb-2014 + 6 days = 25-Feb-2014 $iNoDaysAdd = 6; $date->sub(new DateInterval("P{$iDayOfWeek}D"));// getting start date of the week $sStartDateOfWeek = $date->format('Y-m-d');// getting start date of the week $date->add(new DateInterval("P{$iNoDaysAdd}D"));// getting end date of the week $sEndDateOfWeek = $date->format('Y-m-d');// getting end date of the week //week of the year $iWeekOfTheYearWeek = (string) $date->format('YW'); //week of the year with year $iWeekOfTheYearWeekWithPeriod = (string) $date->format('Y-W'); // getting 4 weeks ago i.e. no. of days to substract $iDaysAgo = ($iWeeksAgo*7) + $iNoDaysAdd + $sWeekDayStartOn; // getting 4 weeks ago i.e. no. of days to substract $date->sub(new DateInterval("P{$iDaysAgo}D")); // getting 4 weeks ago start date i.e. 19-Jan-2014 $sStartDateOfWeekAgo = $date->format('Y-m-d'); // getting 4 weeks ago end date i.e. 25-Jan-2014 $date->add(new DateInterval("P{$iNoDaysAdd}D")); // getting 4 weeks ago start date i.e. 25-Jan-2014 $sEndDateOfWeekAgo = $date->format('Y-m-d'); //ago week of the year $iProccessedWeekAgoOfTheYear = (string) $date->format('YW'); //ago week of the year with year $iProccessedWeekOfTheYearWeekAgo = (string) $date->format('YW'); //ago week of the year with year $iProccessedWeekOfTheYearWeekWithPeriodAgo = (string) $date->format('Y-W'); $aWeeksDetails = array ( 'weeksago' => $iWeeksAgo, 'currentweek' => $iWeekOfTheYear, 'currentdate' => $sCurrentDate, 'startdateofcurrentweek' => $sStartDateOfWeek, 'enddateofcurrentweek' => $sEndDateOfWeek, 'weekagoyearweek' => $iProccessedWeekAgoOfTheYear, 'startdateofagoweek' => $sStartDateOfWeekAgo, 'enddateofagoweek' => $sEndDateOfWeekAgo ); return $aWeeksDetails;

return to bloglist