Addev
Addev

Reputation: 32231

Get the Monday and Sunday of a given week in php

Given a week of the year (20 for example)

How can I retrieve the Monday and Sunday days of this week?.

What i need to show is a string with the day range of that week, something like

 from dayX of monthX of 2012 to dayY of monthXY of 2012

Thanks

Upvotes: 0

Views: 2109

Answers (2)

HamZa
HamZa

Reputation: 14921

Just playing a little with strtotime() and date() ...

<?php

 $week_number = 20;
 $year = 2012;
 $monday = strtotime($year."W".str_pad($week_number,2,0,STR_PAD_LEFT)."1");
 $sunday = strtotime($year."W".str_pad($week_number,2,0,STR_PAD_LEFT)."7");

 echo "From " . date("l", $monday) . " " . date("j", $monday) . " of " . date("F", $monday) . " of " . $year . " to " . date("l", $sunday) . " " . date("j", $sunday) . " of " . date("F", $sunday) . " of " . $year;

?>

Hope this was helpful...

Upvotes: 1

Neil
Neil

Reputation: 55382

The fourth of January is always in week one, so once you know what week day it is you can find the Monday and Sunday of week one. Then just add seven days for each subsequent week.

Upvotes: 0

Related Questions