time() returns different timestamps based on server - php

I searched now for many hours, it seem's like nobody got that problem before.
I run a script, which writes the current timestamp into a database, on two servers. Both have the same os, software, ... and the same timezone.
Now I found out, that the diff between some timestamps and the current time() is a negative number (yes, the calculation is correct: time() - $older_timestamp)
I dumped time() on both servers, the result: it differs by exactly one hour.
Check it out:
time() on server #1: -1 hour
time() on server #2: correct timestamp
Since time() should be always UTC-based, I can not explain, how this happens.
Does someone have any idea?
Greetings from Aachen

Apparently server one and server two are set to different times. Check BIOS and your servers' OS time.

To add to the merry fest, I give you another option.
The OS' Time-Zone might be set incorrectly. If both clocks show the same time but one server thinks it is in London while the other thinks it is in Aachen, the UTC time returned from the system call will differ by one hour.
If you would however install say NTPD none of this would happen. (usually)

This is likely caused by the timezone being incorrectly set in the php.ini for Apache. Check the php.ini file for date.timezone = and change it to your timezone. A list of PHP timezones can be found here: http://php.net/manual/en/timezones.php
For example:
date.timezone = 'America/Chicago'
You may also be able to use an ini_set if you cannot edit the php.ini file directly.
ini_set('date.timezone', 'America/Chicago');

Related

Why date ("c") produce this strange result?

$now= date ("c");
I used a local Apache server and the result:
2015-01-12T23:12:00+08:00
Now here is a problem
Now, at my computer the hour is 22:12 not 23:12
Also my time zone is Jakarta which is +7 not +8
So why does the code produce 2015-01-12T23:12:00+08:00
My apache installation got the timezone somewhere. Somewhere wrong. Where?
Try adding date_default_timezone_set("Asia/Jakarta") at the very top of your PHP file. If that works, change your php.ini to always use the correct timezone (date.timezone="Asia/Jakarta"). That way, you'll reflect the correct timezone of the server (in this case your computer).
To see what the time is in another timezone, you could do what N.B. suggested in the comments to this answer:
$dt = new DateTime("now", new DateTimeZone('Europe/Paris')); //If that's the timezone.
echo $dt->format('c');
I think it's server related stuff and you have to reconfigure the timezone of the server. In php.ini check out the value of date.timezone.

PHP: How to read the current date/time from the server, not from php.ini

Here is my problem:
echo date('Y-m-d H:i:s');
echo date('Y-m-d H:i:s', mktime());
echo exec('date');
The output is:
2012-03-21 08:45:51
2012-03-21 08:45:51
Wed Mar 21 10:45:51 EDT 2012
Server time is off 2 hours from the time returned by php date(); or any other php date/time function. It happens because server time set to EST and PHP.INI date.timezone="America/Denver"
I need to synchronize those two, by using date_default_timezone_set, but I don't know in advance what is the difference.
Is there any other way to get local server time besides calling exec?
UPD: I know that php.ini setting is wrong and that I can change it. The problem is that this script will work on nobody knows what kind of servers. I can't go to each and every one of them and correct the php.ini file. I also don't know in advance what timezone will be on those servers. I need a dynamic solution that will work everywhere.
you can change the ini date time zone and print the date
ini_set('date.timezone', 'America/Los_Angeles');
Change value of date.timezone from php.ini [Date] and restart your server.
You can get your date.timezone value form-
http://au.php.net/manual/en/timezones.php
For Bangladesh I set in my php.ini [Date]
date.timezone = Asia/Dhaka
You get your php.ini in C:\xampp\php address for XAMP server and Windows.
OR
some hosts give you possibility to edit php.ini
look for php config in cpanel
On *nix, you can use formatting parameters to date to get what you need:
date +%z — timezone (hhmm)
date +%:z — timezone (hh:mm)
date +%Z — timezone abbreviation (e.g. "EDT")
Making a system call (e.g. echo exec('date +%z');) will bypass any INI settings as per date_default_timezone_get. Note that this function issues an E_WARNING if it reads from the system time, and indeed from PHP 5.4 it doesn't even allow reading from it — specifically because it can't be relied upon.
To be consistent, regardless of the server settings you should use UTC within your application. For ease of use, GMT is close enough to UTC for most purposes, so you can use gmdate().

PHP wrong date/time

PHP date() & time() return incorrect time:
When date.timezone = "Europe/Riga" the time returned by date() was 03-12-2011 08:57:12, but system time was 03-12-2011 01:57:12 (timezone Europe/Riga - correct time at that moment).
When I changed timezone to "Europe/London", the time changed to 03-12-2011 06:57:12 ( actual time 02-12-2011 23:57:12 )
Time returned by date / hwclock --show was correct (03-12-2011 01:57:12 with system timezone set as Riga)
OS: Debian 6.0
I have checked most of the questions regarding similar issues on SO/Google, but they all seem to have wrong timezone specified.
As far as I can tell there is problem between php -> os.
Of course, because the incorrect time offset is always constant I could subtract difference, but it is not a proper solution.
Any ideas will be greatly appreciated.
Reading PHP manual seems that behaviour of date.timezone is affected by settings in php.ini.
There is another way to set the default timezone in all date/time function and it's the date_default_timezone_set. Try to set it with:
date_default_timezone_set('Europe/Riga');
instead of your date.timezone code.
The problem looks similar to what I have seen on one of my servers. Looks to like bug in php 5.3.2-1. Try to run the php script in the bug report and post your results.
The timezone of the system could be wrong. This results in a shift in a time given by the PHP date() function, althought both php date.timezone (in php.ini) and system time of the server are correct.
I had some problem with timezone too. This can be handy for someone.
In my case, Chile Sumer Time CLST returned wrong timzeone offset.
Updating the timezonedb worked for me.
Go to: https://pecl.php.net/package/timezonedb
For Widnows Download the newest version of dll, copy into "ext" directory. Edit the php.ini and put below line:
extension=php_timezonedb.dll
For Linux You can use :
pecl install timezonedb
and in php.ini put:
extension=php_timezonedb.so
This comment for Azerbaijan timezone
I have problem with 'Asia/Baku' on php 7.2 or php 7.1 version.
I solved problem change to 'Europe/Baku'
You have to set date.timezone in php.ini and restart your server
http://php.net/manual/en/timezones.php
PHP time is based on epoch time scale which uses GMT and later UTC. Most people now refer to it as unix time. Because PHP uses unix time, timezones are not used. I believe subtracting the timezone hours in seconds is the correct method to adjust for the differences.

Why is my PHP error log lagging behind by 2 hours?

When checking php.ini, from both phpinfo and php -i on the command line, my timezone is set to Africa/Johannesburg. So my timezone is GMT +2.
I wrote a tiny snippet to check the time is right as follows:
echo date('h:i A', time());
echo '<br />' . ini_get('date.timezone');
and this outputs the correct time, matching with my localmachine's system time.
However, when I look at my php error log, the time on each error log item is behind by exactly two hours? Why is this, and how can I fix this.
Also having issues with Zend Cache, and i think this time issue is causing it.
It sounds like PHP is configure with one timezone, and the server it is running on is set to a different time zone (possibly GMT / UTC). This means all file operations etc will be working on a different timezone to your PHP scripts.
Check the system timezone of your server.
Google-fu: read what the PHP official site writes.

mysql time and php time not the same

When I use current_timestamp with MySQL, I get the correct time, but when I use
$mysqldate = date( 'Y-m-d H:i:s' );
I get the date with with an hour delay (eg, 4:42 PM is 5:42 PM).
I understood that both function use the server's local time - can someone explain the difference?
Thanks.
The global system time will be set via the /etc/localtime file, which will be either a symlink or a copy of a zone file from /usr/share/zoneinfo/ on most systems. Applications will use this as their default.
PHP can override this in a couple different ways:
date.timezone in php.ini
setting the TZ environment variable, e.g. putenv("TZ=US/Central");
date_default_timezone_set function (in PHP >= 5.1.0)
MySQL can override this by running the following query immediately after connecting:
SET time_zone = 'US/Central'
There are php locale settings, it takes it from php.ini, not from system time
Maybe your PHP server thinks it's in a different time zone or it uses different locale and daylight saving time rules.

Categories