drpogkqqi536984960 2014-05-15 20:02
浏览 105
已采纳

PHP,日期偏移1天。 从DOY(Y-z)格式转换为字符串/ unix

I'm required to take a date in the format 'Y-z' which is year-doy (e.g, 2013-146) and convert that into a unix time stamp to be store into a database.

The issue i have is that i input 2013-146 and turn it into a DateTime Object. then when i output this date in unix or 'Y-m-d' format i get 2013-5-27 not 2013-5-26 which is the correct day.

You can verify the DOY on this NASA website and this NOAA website.

Summary: --I have the date: '2013-146' --Using DateTime::createFromFormat and echoing using 'Y-m-d' and 'Y-z' i get: 2013-5-27 and 2013-146 respectively. --This does not agree with the NASA website I listed and is offset by one day can anyone verify that I'm not losing my mind?

Here is the code you can test:

<?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;
?>
  • 写回答

1条回答 默认 最新

  • dongyi2083 2014-05-15 20:08
    关注

    The problem is than z format in PHP begins with 0 and not with 1.

    Look at: http://www.php.net/manual/en/function.date.php

    z: The day of the year (starting from 0)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号