Is my server time incorrect? - php

Here's what my server says:
date('c') = 2012-08-09T22:11:13-04:00
time() = 1344564673
Within 10 seconds, here's what http://www.unixtimestamp.com says:
THE CURRENT UNIX TIME STAMP
1344568431 EST (-5 GMT + DST when appropriate)
1344564831 UTC (GMT)
...seconds since Jan 01 1970.
This translates to current server time of 08/09/2012 # 10:13pm in EST.
... and when I type my server's time stamp of 1344564673 into unixtimestamp's converter I get:
TIME STAMP: 1344564673
DATE (M/D/Y # h:m:s): 08 / 09 / 12 # 9:11:13pm EST
... My server's off by almost an hour, right? Or am I missing something? (I don't care if it's off by a few minutes)

Your time appears correct (within a few minutes).
It appears that unixtimestamp.com isn't taking DST into effect, which it is right now in EST. That's why their time is off by an hour from what you get from PHP.
Instead try epochconverter.com which does handle DST.
Just FYI in case you didn't already know: time() always returns timestamps in UTC. When you output them in PHP using date(), the output is reflected in the timezone currently set in PHP. This can be set in php.ini using the date.timezone setting, and you can switch it at runtime using date_default_timezone_set().
Hope that helps.

Related

php: DateTime() and time() have different time

I'm using Vagrant box (Homestead) where the timezone is set to Europe/Brussels.
Current time that I get in terminal using command "date", returns this value:
Sun Mar 25 23:27:40 CEST 2018
In both cli & fpm php.ini file I've:
date.timezone = "Europe/Brussels"
Running following code simultaneously:
print_r(new DateTime());
print_r(time());
Gives these results:
For DateTime():
[
"date" => "2018-03-25 23:27:43.650908",
"timezone_type" => 3,
"timezone" => "Europe/Brussels"
]
And the epoch timestamp for time()
1522013263
Converting the latter gives:
Sun, 25 Mar 2018 21:27:43 GMT
Given the results from DateTime I can assume that the timezone settings in php.ini are correct. But then why is the time between DateTime and time is different?
I'm not very experienced with PHP myself, but for time() I see GMT. Is GMT the Time zone you are using, or UTC (or something else)? They may be completely seperate.
If i am incorrect, try reading here
Or for more advanced information, Try here.
time() returns the Unix timestamp and is always in GMT:
Returns the current time measured in the number of seconds since the
Unix Epoch (January 1 1970 00:00:00 GMT).
And 21:27:43 GMT is equal to 23:27:43 CEST.
As noted in the comments: Actually the unix timestamp is in UTC, but PHP does not seem to make much of a difference. Unfortunately the documentation is not very enlightening in regards to this.

Php strtotime returns wrong timestamp

I do
strtotime("2008-09-04")
Where 09 is the month and 04 is the day and I get the result:
1220500800
Which is Thu, 04 Sep 2008 04:00:00 GMT. Where does those 4 hours come from? I should get 1220486400 instead of 1220500800 from strtotime.
You can set timezone globally with function date_default_timezone_set('UTC');, or you can just set timezone locally when you call strtotime() function like:
echo strtotime("2008-09-04 +0000"); # 1220486400
As people above have said, you're likely suffering from a time zone mismatch. This function may be of use in debugging the issue: http://us3.php.net/date_default_timezone_get
The most common problems with PHP's strtotime are timezones and date-time formats. I will address those 2 points.
First the format. As suggested by others on stackoverflow use the iso 8601 date format YYYY-MM-DD (https://www.iso.org/iso-8601-date-and-time-format.html). For example, September 27, 2012 is represented as 2012-09-27.
Next the timeszones. The best of all , do not use any timezone use the Universal Coordinated Time UTC (which is universal time and corresponds with GMT without Daylight Saving). UTC is the time standard commonly used across the world. The world's timing centers have agreed to keep their time scales closely synchronized - or coordinated - therefore the name Coordinated Universal Time (https://www.timeanddate.com/time/aboututc.html).
So now we have the picture clear. To convert a date time into unixtimestamp do:
// Saves your local set Timezone.
$saveDDTZ = date_default_timezone_get();
// Sets the timezone to UTC
date_default_timezone_set("UTC");
// Converts a date-time into a unix timestamp (= 1560470400).
$unixTS = strtotime( "2019-06-14 00:00:00 UTC");
// Restore the timezone
date_default_timezone_set($saveDDTZ");
To restore a unix timestamp to a Date use:
// Saves your local set Timezone.
$saveDDTZ = date_default_timezone_get();
// Sets the timezone to UTC
date_default_timezone_set("UTC");|
$unixTS = 1560470400
$dateTime = date("Y-m-d H:i:s", $unixTS);
// Restore the timezone
date_default_timezone_set($saveDDTZ");
If you want to change the UTC date into a TimeZone use the difference of the UTC and the TimeZone and the Daylight Saving.

Calculate difference between server and user time

I'm writing a simple javascript to calculate the time difference between server and user time. But something is going wrong.
If I catch the javascript and php date i have:
date("M d Y h:i:s A")
php date : Wed Jun 27 2012 04:10:41 AM
new Date()
J S date : Wed Jun 27 2012 10:10:40 GMT+0200 (CEST)
This is correct! I have two different time for local and server time.
Now if I take the seconds time... something goes wrong:
(php: date("U"))
sec PHP: 1340784640
(js new Date().getTime()/1000 )
sec J S: 1340784640
I got the same time!
Can you help me to fix it ?
Thanks!
date("U") and new Date().getTime() return the Unix timestamp which is defined as seconds elapsed since January 1st, 1970 UTC. The current locale's timezone is not taken into account.
Use date('Z') to get the timezone offset in PHP (in seconds) and new Date().getTimezoneOffset() in JavaScript (in minutes).

PHP Date / GMT Weirdness

Either I'm losing my mind, or I've not got the faintest idea what I'm doing. I'm leaning towards the latter.
I'm trying to convert this: 1316826000, which I'm pretty confident should be Sat, 24 Sep 2011 01:00:00 GMT
http://www.onlineconversion.com/unix_time.htm confirms this.
http://www.unixtimestamp.com/index.php tells me 09 / 23 / 11 # 8:00:00pm EST, so far so good. I happen to be in EST, this is the result I'd like to get back from PHP.
When I do date('l, M d, Y, h:ia', $iTime), I get: Friday, Sep 23, 2011, 12:00am, a full 20 hours off.
I've confirmed the server's time is correct using date('c'). date('c') output is: 2012-05-19T03:19:20+00:00. The server is in the central time zone, where it is currently 10:20pm. May 18.
echo date_default_timezone_get() outputs "GMT" (set somewhere else in the script using date_default_timezone_set('GMT'))
What am I missing? Nothing I've read so far can explain how I'm getting a result 20 hours behind what it should be. Were it an hour fast or slow, I could at least wrap my head around it being some sort of DST idiotry, but 20? Crazyness! Thanks for reading!
Check what your php.ini says for date.timezone.
In unix it is usually here: /etc/php.ini
Then use a proper timezone recognized by PHP:
http://www.php.net/manual/en/timezones.php
date.timezone = 'America/New_York'
Then reload your web server.
Unix time just means the number of seconds since epoch. Has nothing to do with timezones. Timezones simply add or subtract 1 hour (3600 seconds) from the unix time for each zone you move away from GMT.
An example:
$userTimezone = new DateTimeZone('America/New_York');
$gmtTimezone = new DateTimeZone('GMT');
$myDateTime = new DateTime('2014-01-22 11:44', $gmtTimezone);
$offset = $userTimezone->getOffset($myDateTime);
echo $offset;
That will output: -14400 or 4 hours. Which is the difference between New York and GMT
Using some Java code with the Joda-Time 2.3 library, as I don't know PHP…
long m = 1316826000L;
DateTime dateTimeUtc = new DateTime( m * 1000L, DateTimeZone.UTC );
DateTime dateTimeNewYork = dateTimeUtc.toDateTime( DateTimeZone.forID( "America/New_York" ) );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "dateTimeNewYork: " + dateTimeNewYork );
I can tell you that 1316826000 seconds from the beginning of 1970 UTC/GMT (Unix Epoch) is…
dateTimeUtc: 2011-09-24T01:00:00.000Z
dateTimeNewYork: 2011-09-23T21:00:00.000-04:00
So, as the commenter stated, it would be 8 PM in EST but EST was not in effect on that day. DST (Daylight Saving Time) (idiocy, as you correctly noted) was in effect until November 9 of that year (2011). So the time of day is pushed forward one hour, to 9 PM.
In GMT/UTC, that means 1 AM in the morning of the next day.
Standard time in east coast US is 5 hours behind UTC/GMT. With DST it is 4 hours behind UTC/GMT (one hour closer).
Where you got confused:
Your time format/conversion in incorrect.I can't help with that as I don't know PHP.
You should be using a competent date-time library for this kind of work.Date-time work is complicated, tricky, confusing, and error-prone.This question discusses possibilities of Joda-Time (for Java) sorts of libraries for PHP.
You used three-letter time zone codes. Avoid these.Those codes are neither standardized nor unique -- there are common duplicates. Instead, use proper time zone names. In your case of east coast US, "America/New_York". Furthermore, in this case you confused the time zone area and rules (east coast US) with a particular application of those rules (EST). Saying "America/New_York" means "whatever time zone rules were in effect on that date, whereas saying "EST" (if interpreted to mean Eastern Standard Time in US) means "UTC-05:00". So either (a) use a time zone name such as "America/New_York", or (b) use a specific offset such as "-05:00".

php mktime for epoch returns -3600, not 0

If I run the following in PHP:
echo mktime(0,0,0,1,1,1970);
the returned value is -3600, not 0 as I expected.
The server is UK based, it's currently 21 Sep (i.e. BST summertime) (though I wouldn't expect this to affect the epoch timestamp) and per php.info: "Default timezone Europe/London".
Setting the daylight saving time flag also, as follows, gives:
echo mktime(0,0,0,1,1,1970,0); (i.e. the correct DST flag, 0 as 1 Jan not DST/BST)
returns -3600
echo mktime(0,0,0,1,1,1970,1); (the incorrect flag - setting 1 Jan as DST)
returns -7200
echo mktime(0,0,0,1,1,1970,-1); (i.e. DST flag not set - left to PHP to decide)
returns -3600
Does anyone know why the epoch would be returned as -3600, not 0, please?
When it was midnight on Jan 1st 1970 in British Summer Time, it was one hour to midnight in Greenwich Mean Time. Try setting the time zone to UTC instead:
date_default_timezone_set('UTC'); // or just change php.ini
mktime() is based on your current timezone. If you want to create a timestamp based on GMT you have to use the gmmktime() function.
gmmktime(0,0,0,1,1,1970)
code on ideone
Resources :
php.net - gmmktime()

Categories