Get the correct hour for the UNIX timestamp

I think this is a stupid question, but it seems that I can not find the answer.

I have this timestamp: 1295598602.

In my php script, I have:

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

This returns:

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

Now I went to the online conversion site to check this out. 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?

+3
source share
5 answers

Use the correct time zone:

>> 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'
+5
source

GMT/UTC ( ), timestamp Fri, 21 Jan 2011 08:30: 02 GMT.

, GMT, gmdate() date().

+4

. PHP . date_default_timezone_set('UTC'); .

+1

- script.

,

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

, -.

PHP , , .

PHP : http://www.php.net/manual/en/ref.datetime.php

+1

(),

timestamp is optional and the default value is time ().

And according to the description of the time function () , it returns a timestamp in time.

So, PHP does the conversion to your timezone, and onlineconversion.com does not.

0
source

Source: https://habr.com/ru/post/1786868/


All Articles