RSS
 

Posts Tagged ‘year’

conversion of dates

06 Mar

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.

 

Happy New Year

06 Jan

First of all, we are very sorry to leave you waiting for such a long without an interesting post. We hope to start writing real soon and to please your eyes and brain again with great stories, interesting updates and wonderful news. Unfortunately we don’t have a lot of spare time, but we’ll do the best we can.

For now we wish you a great year filled with productive coding and love.

Have a wonderful 2009,

the devexp crew