Nazariy Nazariy - 2 years ago 93
PHP Question

Last day of the leap year, week 53 or 1?

I have a script that process raw CSV data and generate report grouped by week of the year.

Which looks something like this:

//timezone is set to Europe/London
$date = new DateTime($raw['order_date']); // example: 12/31/2012

$key = $date->format('Y W'); // 2012 01

$array[$key][] = $raw['product_id'];

Everything was working fine until I tried to parse data generated over new year eve, for some reason system believes that 31 December 2012 is week number 1 of year 2012.
I'm not sure is it bug or a feature, but produced reports are definitely wrong.

What is a correct way of passing this issue and keeping data grouped by weeks?

Answer Source

Try this:

$key = $date->format('o W');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download