4

im trying to get the first day of a given week based on inputting a week number and year. im currently using this for the current week and year and works like a charm.

$week = date('W');
$year = date('Y');
$date1 = date('Y-m-d', strtotime($year."W".$week.'1'));

however when i try to input my own week and year it doesn't work

$week = 7;
$year = 2013;
$date1 = date('Y-m-d', strtotime($year."W".$week.'1'));

what type if string should i be using for strtotime? how come providing my own date doesn't work?

Sergio
  • 28,539
  • 11
  • 85
  • 132
seesoe
  • 248
  • 6
  • 21
  • 1
    http://stackoverflow.com/questions/1897727/get-first-day-of-week-in-php – fionbio Feb 06 '13 at 06:03
  • http://stackoverflow.com/questions/14722585/php-get-first-day-of-week-of-given-week-and-year#autocomment20596575 [1]: http://stackoverflow.com/questions/923925/get-start-and-end-days-for-a-given-week-in-php – ibrahim.alzein Feb 06 '13 at 06:20
  • here is my code for first and last days of given week `$string = $year.'W'.str_pad($week, 2, '0', STR_PAD_LEFT); $date1 = date('Y-m-d', strtotime($string.'1')); $date2 = date('Y-m-d', strtotime($string.'7'));` – seesoe Feb 06 '13 at 06:43
  • 2
    I don't hing this is a duplicate. The linked question wants a week nr from a specific date, this one is the opposite. – Sergio Sep 15 '14 at 07:57
  • Please nose that "week number" is different across locales: http://www.pjh2.de/datetime/weeknumber/wnd.php?l=en#Legend – Dima Tisnek Sep 15 '14 at 08:28
  • 1
    This question is not really a duplicate and http://stackoverflow.com/questions/1897727/get-first-day-of-week-in-php does not contain an answer how to get the first date of the week from a week number. – bancer Jan 04 '16 at 12:07

1 Answers1

7

Because the week numbers are supposed to be padded (and the extra 1 at the end doesn't really do anything):

$date1 = date(
    'Y-m-d', 
    strtotime($year . 'W' . str_pad($week, 2, '0', STR_PAD_LEFT))
);
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309