15

I'm trying to get the date from the week number, day number and year.

For eg:

week number = 52   
day number = 4 (of week 52)  
year = 2013  

In this case, the date should be 26-12-2013.

How can I do it using PHP? I've already tried with strtotime(), but I'm confused about the formats. Can someone help?

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
Code
  • 1,574
  • 2
  • 23
  • 37
  • What you tried so far? – Alma Do Dec 26 '13 at 07:24
  • I tried with strtotime function , but i am confused with the format.. just we neet time and easy to get date – Code Dec 26 '13 at 07:25
  • @Vineet: open [this link](http://en.wikipedia.org/wiki/ISO_8601), and take a look at the right,where is says `Date with week number: 2013-W52-3`. Have you tried `strtotime()` with parameter like that? `2013-W52-4` – Glavić Dec 26 '13 at 16:25

3 Answers3

43

Make use of setISODate()

<?php
$gendate = new DateTime();
$gendate->setISODate(2013,52,4); //year , week num , day
echo $gendate->format('d-m-Y'); //"prints"  26-12-2013
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
5

Try this code.

 <?php

    function change_date($week_num, $day) {
      $timestamp    = strtotime(date('Y') . '-W' . $week_num . '-' . $day);
      return $timestamp;
    }
   $timestamp = change_date(52, 4);
    echo date('d-m-Y', $timestamp);
?>
Maz I
  • 3,664
  • 2
  • 23
  • 38
  • 2
    You should also give year as parameter. And watch out for week numbers bellow 10, because week number must always have 2 integers. – Glavić Dec 26 '13 at 16:30
1

You can also use the strtotime function. In your example, you can write:

date("Y-m-d", strtotime("Y2013W52-4")) // outputs: 2013-12-26

The strtotime will give you a timestamp that you can use in combination withe the date function.

Sebastian Td
  • 174
  • 1
  • 7