我需要采用“ Yz”格式的日期(该年份为年份)(例如2013-146),并将其转换为unix时间戳以存储到数据库中。
我的问题是我输入2013-146并将其转换为DateTime对象。然后当我以Unix或'Ymd'格式输出此日期时,我得到的是2013-5-27而不是2013-5-26,这是正确的一天。
摘要:-我有日期:'2013-146'-使用DateTime :: createFromFormat并使用'Ymd'和'Yz'回显,我分别得到:2013-5-27和2013-146。-这与我列出的NASA网站不同,并且有一天可以抵消我是否会失去主意吗?
这是您可以测试的代码:
<?php
date_default_timezone_set('America/Chicago');
$year = 2013; //where this outputs a simple year 'CCYY'
$day = 146; //where this provides the day of year
$format = 'Y-z'; //specifying what format i'm creating the datetime with
$date = $year.'-'.$day; //formatting the strings to the above $format
$timezone = new DateTimeZone('America/Chicago'); //specify the timezone
$fileDateStore = DateTime::createFromFormat($format, $date, $timezone);//, $timezone); //create the DateTime object
$fileDateString = date_format($fileDateStore,"Y-m-d"); //format it so strtotime() can read it
$fileDate = strtotime($fileDateString); //finally create the Unix Timestamp for the date.
$newfileDOY = date_format($fileDateStore,"Y-z");
echo 'newfileDOY = '.$newfileDOY.', ';
echo 'date = '.$date.', ';
echo 'fileDateString = '.$fileDateString.', ';
echo 'fileDate = '.$fileDate.PHP_EOL;
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句