Get the correct hour for a UNIX timestamp

10,139

Solution 1

Use correct timezone:

>> date_default_timezone_get();
'UTC'
>> date('Y-m-d h:i:s',1295598602);
'2011-01-21 08:30:02'
>> date_default_timezone_set('CET');
true
>> date('Y-m-d h:i:s',1295598602);
'2011-01-21 09:30:02'
>> date_default_timezone_set('UTC');
true
>> date('Y-m-d h:i:s',1295598602);
'2011-01-21 08:30:02'

Solution 2

In GMT / UTC (they're almost but not quite exactly the same) that timestamp is indeed Fri, 21 Jan 2011 08:30:02 GMT.

If you're in a different timezone but always want GMT you'll need to use the gmdate() function instead of date().

Solution 3

Both are correct. In the code snippet PHP adjusts for timezone. Try date_default_timezone_set('UTC'); to get proper unadjusted values.

Solution 4

Another option is to set the default timezone for your script.

For example,

date_default_timezone_set('Europe/London');
$timestamp = '1295598602';
echo date('Y-m-d H:i:s', $timestamp);

would get you the same result as the online conversion tool is showing.

There are a number of timezone-related function in PHP that will allow you to modify which time zone is being shown.

You can check the PHP docs for a list of your options: http://www.php.net/manual/en/ref.datetime.php

Share:
10,139
CristiC
Author by

CristiC

Self-teaching is the most powerfull instrument of all.

Updated on June 27, 2022

Comments

  • CristiC
    CristiC almost 2 years

    I think this is a stupid question, but seems that I cannot find the answer.

    I have this timestamp: 1295598602.

    In my php script I have:

    $date = date('Y-m-d', 1295598602); $hour = date('H', 1295598602) . ':00';

    This returns:

    Date: 2011-01-21
    Hour: 03:00

    Now I went to an online conversion site to test this. I used this one. But it seems that for this timestamp value it is

    Fri, 21 Jan 2011 08:30:02 GMT

    Now, which one is correct?

  • Alnitak
    Alnitak over 13 years
    date_default_timezone_set() is only in PHP 5.1.0 and later, which may not be available.
  • Mchl
    Mchl over 13 years
    Seriously? PHP 5.2 is now no longer supported by it's developers. If someone is still below 5.1, they should consider upgrading in the first place.
  • Alnitak
    Alnitak over 13 years
    Seriously, yes. I've seen a hosting company that still only has PHP 4.3.2 on its shared hosting web servers.
  • Mchl
    Mchl over 13 years
    Europe/London timezone respects daylight savings. Use UTC or GMT instead.
  • Mchl
    Mchl over 13 years
    So what? I've seen people doing other stupid things. I always assume people work with current (or a least recent) technology. If they don't, they need to state that clearly, so that I can consider if I want to work with them at all, and at what price (because it surely won't be my usual rate).
  • Alnitak
    Alnitak over 13 years
    My point was that if you specifically want GMT there's a perfectly good function which always returns GMT, without faffing with changing the timezone, and which is in every version of PHP.
  • Mchl
    Mchl over 13 years
    A valid point. I just tend to to use timezone setting, mostly because I work moore with DateTime objects. +1
  • Gazzer
    Gazzer over 8 years
    This should be "H:i:s" as the small 'h' is not 24 hour clock. 02:00:00 and 14:00:00 both show as 02:00:00. Doesn't affect the answer though.