Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting Dates From Week Numbers in PHP

DZone's Guide to

Getting Dates From Week Numbers in PHP

· Web Dev Zone ·
Free Resource

Learn how Crafter’s Git-based content management system is reinventing modern digital experiences. Download this white paper now. 

Recently I've been building a little project that pulls data from Google Analytics and shows your web statistics in a simple form. One thing I wanted to do was show the data for a quarter, but graphing by day is too chaotic and graphing by month only gives three points, so I wanted to graph by week. This was fine but the data returned by Analytics only gives me the week numbers, and since this project is aimed at demystifying web stats for normal people, it really needs normal dates on it!

Using DateTime::setISODate

I found that the DateTime extension has a method setISODate() which accepts the year and week number, and makes a date you can then use as normal.

$week_start = new DateTime();
$week_start->setISODate($year,$week_no);
echo $week_start->format('d-M-Y');

You can also do this the other way around; if you have a date in PHP, there's the 'W' flag for date() which will return you the week number - very handy!

 

Source: http://www.lornajane.net/posts/2011/Getting-Dates-From-Week-Numbers-in-PHP

 

Crafter CMS is a modern Git-based platform for building innovative websites and content-rich digital experiences. Download this white paper now.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}