I have a problem with the time function in PHP.
It provides a different time.
For example, the current time in my PC is 10:00AM, it appears 5 hours late..
any thoughts?
Thanks in advance
The server system is configured with the wrong timezone for your location. Fix that with date_default_timezone_set.
can change the timezone in php ini file also
e.g.
[Date]
; Defines the default timezone used by the date functions
date.timezone = "America/Los_Angeles"
Related
I live in the UK and the current time is 02:30am on Monday 10th Feb 2014, however, my website is showing 02:30am Sunday 9th Feb 2014 - the day and date where the same before midnight so i am assuming a timezone difference. Can i set this to my own timezone ?
edit: Can i set this to the users' timezone?
That doesn't look like a timezone issue since the date if off by a full 24 hours. But just in case it is, it is set in your php.ini, and of that is not set, it defaults to the server time zone.
You can set it in your php.ini file by setting date.timezone to be the timezone of your choosing.
date.timezone = Europe/London;
If you don't have access to the php.ini you can use date_default_timezone_set to set the timezone in your scripts.
date_default_timezone_set('Europe/London');
You have two ways to define the timezone you want:
date_default_timezone_set date_default_timezone_set('Asia/Manila');
Modifying your php.ini: date.timezone = Asia/Manila
In code, you can see what the default timezone is by calling date_default_timezone_get(). You can set it programatically using date_default_timezone_set(). The preferred way to do it though is to use the php.ini settings.
I think you have to set the timezone preference in the php.ini file that initializes the PHP environment or if you don't have access to anything like that, you could look into http://www.php.net/manual/en/function.date-default-timezone-set.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');
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 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.
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.