Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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);
?>

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top