Вопрос

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;
?>
Это было полезно?

Решение

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)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top