How to get date from week number & day number and year?
Asked Answered
V

3

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?

Verleneverlie answered 26/12, 2013 at 7:20 Comment(3)
What you tried so far?Gibeonite
I tried with strtotime function , but i am confused with the format.. just we neet time and easy to get dateVerleneverlie
@Vineet: open this link, 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-4Nonstriated
B
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
Beshore answered 26/12, 2013 at 7:26 Comment(1)
What a bout clashes at the end of the year? Is it safe?Pork
S
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);
?>
Sitdown answered 26/12, 2013 at 7:30 Comment(1)
You should also give year as parameter. And watch out for week numbers bellow 10, because week number must always have 2 integers.Nonstriated
S
2

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.

Skycap answered 29/6, 2022 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.