conversion of dates
I struggled with a bit of code last week, but found a nice solution.
The goal was to convert 3 parameters into a date:
1. The day of the week (0 -> 6, 0 = Monday, 1 = Tuesday, 2 = Wednesday, etc.)
2. The week number (0 – 53, see our previous post for more info)
3. And the year (2009)
Well in this function we got to that result.
/**
* create datetime from dow, week number and year
* @param integer $dow (MUST BE 0 -> 6)
* @param integer $weekNumber
* @param integer $year
* @return datetime
*/
public function getDateTimeByDowWeekYear ($dow, $weekNumber, $year) {
// get the first day of the current year, according iso standards
$offset = date('w', mktime(0,0,0,1,1,$year));
$offset = ($offset < 5) ? 1-$offset : 8-$offset;
//get the first Monday of the year
$monday = mktime(0,0,0,1,1+$offset,$year);
//add the number of weeks
$mondayTime = strtotime('+' . ($weekNumber - 1) . ' weeks', $monday);
// add the number of weekdays
$dayTime = strtotime('+' . $dow . ' days', $mondayTime);
//create a date
return date('Y-m-d H:i:s',$dayTime);
}
It seemed to be a lot easier then I thought.
Et voila, have fun.
Newsletter
If you want to be up to date on the important things, related to the topics I talk about, events and the book I'm writing, please subscribe.
Upcoming Events
- March 8, 2012 6:00 pm'Scrum in practice 2012' Training Session n°3
- March 12, 2012Belgium Testing Days 2012
- April 19, 2012 6:00 pm'Scrum in practice 2012' Training Session n°4
Link to Calendar




