简体   繁体   English

php 查找 2010 年第 16 周的开始

[英]php Find the start of week 16 in 2010

Ok so I want to get the Monday of week 16 in 2010 (more precisely to week $week of year $year).好的,所以我想得到 2010 年第 16 周的星期一(更准确地说是一年中的第 16 周)。 here is what I have in my test.php file这是我的 test.php 文件中的内容

$week = 16;
$year = 2010;
$dateString = $dateString = 'Start of week ' . $week . ' in ' . $year;
$date = strtotime( $dateString );
echo $date;

The value returned is false.返回的值为假。 I have also tried using Monday of week... but this also gave a false result.我也尝试过使用每周的星期一......但这也给出了错误的结果。

any idea how to get a date given a week number and the year?知道如何在给定周数和年份的情况下获得日期吗?

thanks谢谢

I'd recommend using the builtin \DateTime class.我建议使用内置的\DateTime类。

Inspired by the answer here I tested the following code:此处答案的启发,我测试了以下代码:

<?php

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

$week = 16;
$year = 2010;

var_dump(getStartDate($week, $year));

This returned the following:这返回了以下内容:

/some/fancy/path/test.php:12:string '2010-04-19' (length=10)

Monday is the first day of the week.星期一是一周的第一天。 Make use of setISODate()使用setISODate()

<?php

$gendate = new DateTime();
$gendate->setISODate(2022,2,1); //year , week num , day
echo $gendate->format('d-m-Y'); //"prints"  10-01-2022

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

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