简体   繁体   English

php:从年周和星期几计算日期

[英]php: Calculate Date from Week of Year and Day of Week

I am currently struggling with the following: I need to get the Date like "Sun, 29.05.2016 18:00" but all i have is the number of the week from the beginning of the year, the number of the day of week and the hour of the day. 我目前正在努力解决以下问题:我需要像"Sun, 29.05.2016 18:00"那样获取日期"Sun, 29.05.2016 18:00"但我所拥有的是从年初开始的星期数,星期几和一天的时间。

Week: 21
Day:  7
Hour: 18

Now my question is how do i get the actual date and time with php's date functions or own code? 现在我的问题是如何使用php的日期函数或自己的代码获得实际的日期和时间?

You are looking for DateTime::setISODate 您正在寻找DateTime :: setISODate

$week_no = 21;
$day =7;
$hour = 18;

$d= new DateTime();
$d->setISODate(date('Y'),$week_no, $day);
$d->setTime ($hour,0);
echo $d->format('D, d.m.Y H:i');

Pretty straightforward with DateTime objects: 使用DateTime对象非常简单:

$week = 21;
$day = 7;
$hour = 18;

$dateFormat = sprintf('%d-W%02d-%d', (new DateTime())->format('Y'), $week, $day);

$x = new DateTime($dateFormat);
$x->setTime($hour, 0);
echo $x->format('Y-m-d H:i:s');

(assuming the current year) (假设当年)

Try with setISODate and DateTime . 尝试使用setISODateDateTime Online Check 在线检查

Setting the 2016 from your desire output and use the hour directly to the format. 从您的愿望输出设置2016并将小时直接用于格式。

$gendate = new DateTime();
$gendate->setISODate(2016, 21, 7); //year , week num , day
echo $gendate->format('D, d.m.Y 18:00'); //Sun, 29.05.2016 18:00

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

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