6

I know the year and the day of year (0-365). Is there any way to get back from it to timestamp?

Something like:

$day=date('z');
$year=2012;

$timestamp=strtotime($day.'-nd day of '.$year);
Narek
  • 3,813
  • 4
  • 42
  • 58

3 Answers3

5

Try the following:

$timestamp = mktime(0,0,0,1,$day,$year);

It will produce a timestamp where the time is set to 0:00:00. (I am not sure if it will behave correctly with respect to daylight savings though...)

Check the PHP manual for further details: PHP: mktime - Manual

Waboodoo
  • 508
  • 4
  • 17
2

Maybe strtotime gets handy here: php -r 'echo @date("Y-m-d H:s", strtotime("january 2012 +200 day")).PHP_EOL;' In your example $timestamp = strtotime("january $year +$day day");

core1024
  • 1,882
  • 15
  • 22
1

Try using createFromFormat :

$date = DateTime::createFromFormat('z Y', $day . ' ' . $year);
echo $date->getTimestamp();
Manse
  • 37,765
  • 10
  • 83
  • 108
  • I liitle afraid to use classes like this, because in some servers they didn't work, but in future this will be the best solution I think. Thanks! – Narek May 10 '12 at 11:43