-3

I can`t figure out the code to get the date from monday depending on the week number of this year.

could someone help me with this code?

thanks.

3 Answers3

2

This will provide the Monday of a certain week this year:

$thisYear = date('Y');
$weekNum = 40;

$date = date('Y-m-d', strtotime("$thisYear-W$weekNum-1"));  // Outputs 2013-09-30
aynber
  • 22,380
  • 8
  • 50
  • 63
  • 1
    Yeah I like this one more than mine. The flexibility of strtotime amazes me every time I see it used a new way. – emsoff Sep 03 '13 at 18:58
  • I can't remember where I found it, but it comes in handy when trying to compare information from a Mon-Sun week of this year to the same Mon-Sun week of last year. – aynber Sep 03 '13 at 19:51
  • If you are using this code, be carefull with week number, it must be two digit long; always prepend 0 if week number is bellow 10. – Glavić Sep 21 '13 at 19:24
1

Use DateTime class :

$dt = new DateTime;
$dt->setISODate($year, $week);
echo $dt->format('Y-m-d');

demo

Glavić
  • 42,781
  • 13
  • 77
  • 107
-1

I have this laying around (I'm not sure if I or someone else was the original author) but give it a try:

  $week = 34; //Enter your week here.

  $week = mktime( 0, 0, 0, 1, 1,  2013 ) + ( $week * 7 * 24 * 60 * 60 ); 

  $monday = $week - 86400 * ( date( 'N', $week ) - 1 );
  $monday = date( 'Y-m-d', $monday );

  echo $monday;
emsoff
  • 1,585
  • 2
  • 11
  • 16