简体   繁体   English

如何从周数和日数和年份获取日期?

[英]How to get date from week number & day number and year?

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 .在这种情况下,日期应该是26-12-2013

How can I do it using PHP?我怎样才能使用 PHP 做到这一点? I've already tried with strtotime() , but I'm confused about the formats.我已经尝试过strtotime() ,但我对格式感到困惑。 Can someone help?有人可以帮忙吗?

Make use of setISODate()使用setISODate()

<?php
$gendate = new DateTime();
$gendate->setISODate(2013,52,4); //year , week num , day
echo $gendate->format('d-m-Y'); //"prints"  26-12-2013

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.您还可以使用strtotime函数。 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. strtotime 将为您提供一个时间戳,您可以将其与 date 函数结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM