简体   繁体   English

使用PHP查找工作日,工作日和工作日的日期

[英]Find date out from weekday, week and year with PHP

I have a day of the week number 'N' (1 for Monday through 7 Saturday) and a week 'W' and a year 'Y'. 我有星期几“ N”(1表示星期一至星期六7),还有星期“ W”和年份“ Y”。

How can i find the date? 我如何找到日期?

Day of the week: 4 Week: 35 Year: 2014 星期几:4周:35年:2014年

Should output: 2014-08-28 (Thursday) 应该输出:2014-08-28(星期四)

I have tried using strotime() without any luck. 我尝试使用strotime()时没有任何运气。 I cannot seem to find a way to specify the week and year, together with the weekday number. 我似乎找不到找到指定星期和年份以及工作日编号的方法。

Just did my own solution. 只是做了我自己的解决方案。 Here is how you could do it. 这是您的方法。

function getDayDate($day, $week, $year) {
  $day -= 1;

  $dto = new DateTime();
  $dto->setISODate($year, $week);
  $dto->format('Y-m-d') . ' 00:00:00'; // start of the week date
  $dto->modify('+'.$day.' days'); // add the days
  $result = $dto->format('Y-m-d') . ' 23:59:59';
  return $result;
}

$getdate = getDayDate(4, 35, 2014);

echo $getdate;

You can use DateTime 's setISODate() method for exactly this purpose. 您可以setISODate()使用DateTimesetISODate()方法。 Your solution is almost right, but I don't understand why you're not supplying the weekday argument right from the start. 您的解决方案几乎是正确的,但是我不明白为什么您从一开始就没有提供平日的论点。

function get_date($weekday, $week, $year) {
    return (new DateTime())->setISODate($year, $week, $weekday)->format('Y-m-d');
}

var_dump(get_date(4, 35, 2014));

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

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