RSS
 

Posts Tagged ‘date’

A few helpful PHP date methods

14 Apr

For a lot of people dates in PHP are an issue, and the PHP functions aren’t sufficient enough to do all converts you need.
Therefore you find a few easy PHP date conversion methods. Feel free to add your own methods or requests in the comments as we know that these methods don’t cover everything. You can see it for yourself when you read the rest of this entry:

  • get start and end date for the next two months in an array
  • get the datetime for a specific user culture in Symfony
  • get all days between a given start and end date and return it in an array
  • get the difference between two dates

Read the rest of this entry »

 

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.