gpt4 book ai didi

Php如何从一年中的某一天转到日期,反之亦然

转载 作者:行者123 更新时间:2023-12-03 00:48:40 27 4
gpt4 key购买 nike

如何在 php 中从一年中的第 n 天到这样的日期:

getdatefromday(275, 2012) 

它输出一个日期(如果是一个对象更好)。

我也想做相反的事情,比如getdayoftheyear("21 oct 2012")

最佳答案

这一切都很简单。您应该阅读 DateTime 对象的 createFromFormat 静态方法 here日期函数herestrtotime 函数 here .

// This should get you a DateTime object from the date and year.
function getDateFromDay($year, $dayOfYear) {
$date = DateTime::createFromFormat('z Y', strval($year) . ' ' . strval($dayOfYear));
return $date;
}

// This should get you the day of the year and the year in a string.
date('z Y', strtotime('21 oct 2012'));

关于Php如何从一年中的某一天转到日期,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9735528/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com