php shows one hour early - php

php settings in php.ini:
date.timezone = Asia/Jerusalem.
My Linux server (Ubuntu) shows the correct time: Wed Oct 1 15:35:39 IDT 2014
But when I echo date('Y-m-d H:i:s'); I get 2014-10-01 14:35:39 (One hour earlier).
I also tried to date_default_timezone_set('Asia/Jerusalem'); but the results are the same
Why is the result off?

Israel has undergone several changes to daylight saving time in the last few years. This year, DST is supposed to be in effect until October 26th. The last time the DST end date fell before October 1st was September 23, 2012. Reference here.
When Israel announced the changes for 2013 forward, they were released in the standard IANA time zone database in version 2013d. I actually answered a similar question back then, regarding how Java handles this change.
For PHP, the IANA database is implemented via the timezonedb PECL package. You can install this package manually to update to the latest version of the timezone data.
PHP also comes with a copy of timezonedb internally, which is supposed to be the most recent version available at the time that the PHP version is released. So simply upgrading PHP to the latest version should also solve the problem.
If you want to see what version of timezonedb you currently have (whether built-in, or updated) - you can call timezone_version_get(), as documented here. It needs to be at least 2013.4 to have this particular change. The current release is 2014.7.

You solve it by setting the timezone explicitly in your PHP scripts. You can do this with date_default_timezone_set():
date_default_timezone_set('Asia/Jerusalem');
Here is the list of PHP supported timezones.
You may also want to try a test script calling date_default_timezone_get() to see what it's actually set to to verify that this is in fact the problem.
List of Supported Timezones

Set and check your time zone in php.
<?php
date_default_timezone_set('America/Los_Angeles'); // here set the time zone
$script_tz = date_default_timezone_get();
if (strcmp($script_tz, ini_get('date.timezone'))){
echo 'Script timezone differs from ini-set timezone.';
} else {
echo 'Script timezone and ini-set timezone match.';
}
?>

The Asia/Jerusalem timezone has daylight savings time currently in effect; it may be that php isn't handling this. The function call date('I') will return 1 if your php is currently running DST, 0 otherwise.

Change the php timezone:
1) Open the php.ini file - vim /etc/php.ini
2) Change the default timezone settings by adding/modifying this line:
date.timezone = Asia/Jerusalem - remove the ; at the first of line (if exists)!
3) Save the php.ini file.
4) Restart the Apache server - systemctl restart httpd.service.
The timezone settings should now be modified.

Related

Updating Timezone Information on PHP [duplicate]

php settings in php.ini:
date.timezone = Asia/Jerusalem.
My Linux server (Ubuntu) shows the correct time: Wed Oct 1 15:35:39 IDT 2014
But when I echo date('Y-m-d H:i:s'); I get 2014-10-01 14:35:39 (One hour earlier).
I also tried to date_default_timezone_set('Asia/Jerusalem'); but the results are the same
Why is the result off?
Israel has undergone several changes to daylight saving time in the last few years. This year, DST is supposed to be in effect until October 26th. The last time the DST end date fell before October 1st was September 23, 2012. Reference here.
When Israel announced the changes for 2013 forward, they were released in the standard IANA time zone database in version 2013d. I actually answered a similar question back then, regarding how Java handles this change.
For PHP, the IANA database is implemented via the timezonedb PECL package. You can install this package manually to update to the latest version of the timezone data.
PHP also comes with a copy of timezonedb internally, which is supposed to be the most recent version available at the time that the PHP version is released. So simply upgrading PHP to the latest version should also solve the problem.
If you want to see what version of timezonedb you currently have (whether built-in, or updated) - you can call timezone_version_get(), as documented here. It needs to be at least 2013.4 to have this particular change. The current release is 2014.7.
You solve it by setting the timezone explicitly in your PHP scripts. You can do this with date_default_timezone_set():
date_default_timezone_set('Asia/Jerusalem');
Here is the list of PHP supported timezones.
You may also want to try a test script calling date_default_timezone_get() to see what it's actually set to to verify that this is in fact the problem.
List of Supported Timezones
Set and check your time zone in php.
<?php
date_default_timezone_set('America/Los_Angeles'); // here set the time zone
$script_tz = date_default_timezone_get();
if (strcmp($script_tz, ini_get('date.timezone'))){
echo 'Script timezone differs from ini-set timezone.';
} else {
echo 'Script timezone and ini-set timezone match.';
}
?>
The Asia/Jerusalem timezone has daylight savings time currently in effect; it may be that php isn't handling this. The function call date('I') will return 1 if your php is currently running DST, 0 otherwise.
Change the php timezone:
1) Open the php.ini file - vim /etc/php.ini
2) Change the default timezone settings by adding/modifying this line:
date.timezone = Asia/Jerusalem - remove the ; at the first of line (if exists)!
3) Save the php.ini file.
4) Restart the Apache server - systemctl restart httpd.service.
The timezone settings should now be modified.

date_default_timezone_set displays incorrect time

I'm using date_default_timezone_set function to set the timezone but when I echo the time it displays wrong time on local server xampp
date_default_timezone_set ('Africa/Cairo');
echo "<div style='direction:ltr'>".date("Y-m-d / g:i A")."</div>";
the time now is 10:50 am but it displays 9:50 am. So any ideas?
Possible issues:
the server's time is not set correctly, test this by confirming that time in the UTC timezone is correct
the timezonedb is outdated and the timezone in question has changed its DST rules in the meantime, update your PHP version and/or your timezonedb via PECL

How to change default GMT timezone of php 5.4 application on OpenShift?

I recently subscribed to OpenShift and deployed a new php 5.4 application, everything works fine, but I can't seem to change the default GMT php timezone.
Here's where this GMT timezone is defined:
> grep timezone ~/php/configuration/etc/php.ini
date.timezone = GMT
I'm able to edit this php.ini file and update the timezone to my desired value (America/Montreal), but as soon as the php cartridge is restarted (simply by doing a git push or issuing 'ctl_app restart' command on the server for ex), there's an unknown process that restores the php.ini back to openshift factory defaults, so I loose my changes and the timezone is back to GMT.
Any idea?
Thanks
You can use ini_set() to modify php.ini settings at run time. The modification will be valid only for the current run.
ini_set('date.timezone', 'america/new_york');
ini_set('date.timezone', 'europe/london');
ini_set('date.timezone', 'america/sao_paulo');
... etc.
This setting will affect behaviour of date and time functions like date().
Here is a list of supported timezones.

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.

Categories