date function disregards the default timezone - php

Following best practice to not modify php.ini directly but to have separate files, I have the following file:
$ cat /etc/php/7.0/apache2/conf.d/99-timezone.ini
[Date]
date.timezone="America/Los_Angeles"
I can see that the ini file is being loaded as evidenced by this phpinfo screenshot. However in the date section only one of the two directives has updated to show America/Los_Angeles and one is UTC. I don't even know where UTC is coming from, as the system's time is not UTC:
$ timedatectl
Local time: Tue 2017-11-07 18:36:56 PST
Universal time: Wed 2017-11-08 02:36:56 UTC
Timezone: America/Los_Angeles (PST, -0800)
(snip)
I see the following outputs when I run each of these:
var_dump(ini_get('date.timezone')); //string(19) "America/Los_Angeles"
var_dump(date_default_timezone_get()); //string(3) "UTC"
var_dump(date('e')); //string(3) "UTC"
How do I get the first two to local time?

Looking at your second screenshot, the only way for that to happen (that I know of) is if date_default_timezone_set('UTC'); has been called at runtime. It alters the 'Default Timezone', causing it to differ from the date.timezone value in your .ini files.
So look for things like:
auto_prepend_file= in your .ini file. This directive causes a script to be automatically prepended before every PHP file that is processed. If this is in use and the PHP file that it loads sets the timezone, it could cause this.
Are you using a PHP framework? If so, search the framework's PHP files for date_default_timezone_set. As an example, WordPress forces the timezone to UTC.
If not using a framework, are you using any PHP libraries that may contain date_default_timezone_set. If so, that could be the trouble.
The fact that it's showing up like this in your phpinfo() output suggests it is an auto_prepend_file doing this, because I'm assuming that you're checking phpinfo with a file that only contains <?php phpinfo(); — no framework or library.

Related

Why is the default PHP time zone "Europe/Moscow" in my system?

I have been running PHP on many Ubuntu versions and all of them exhibit the same "feature". When I dump the output of the phpinfo() function to a web page, I see the following:
Default timezone: Europe/Moscow
when both php.ini files (/etc/php/7.?/apache2/php.ini and /etc/php/7.?/cli/php.ini) have the date.timezone setting commented out, as it is the case by default.
My system's time zone has always been set correctly to Europe/Istanbul:
$ file /etc/localtime
/etc/localtime: symbolic link to /usr/share/zoneinfo/Europe/Istanbul
However, PHP thinks my local time zone is Europe/Moscow instead. Currently, these two time zones are similar, but this was not so in the past and may not be so in the future.
To solve, this problem each time these php.ini files are being updated (for example, after a patch or system upgrade) I have to manually edit them and set:
date.timezone = Europe/Istanbul
What is the reason of this strange behavior and what can I do to solve this problem?
Currently, I am on Ubuntu 19.10 with PHP 7.3, but the problem exists in previous OS and PHP versions too.
You said yourself that Moscow and Istanbul are in the same timezone. If you are worried that your PHP programs may suffer functionality in the event that this were to ever change you can may use the function
date_default_timezone_set ( string $timezone_identifier ) : bool
to override the php.ini file. I have included a link to the documentation.

php printing wrong timezone

I am in Kuala Lumpur, but when I print current time zone in php it is showing:
Europe/Berlin
echo date_default_timezone_get();
isn't it supposed to show Asia/Kuala_Lumpur?
I am using localhost
Can you check your php.ini file, there is an option to configure your timezone.
date.timezone=[Set your Value]
You can get the timezone values you can configure on the link below:
http://php.net/manual/en/timezones.php
For your case it should be: Asia/Kuala_Lumpur
date_default_timezone_get returns the default timezone by:
Reading the timezone set using the date_default_timezone_set()
function (if any)
If PHP >= 5.4.0: Reading the TZ environment variable (if non empty).
Reading the value of the date.timezone ini option (if set)
Manual:
Prior to PHP 5.4.0 only: Querying the host operating system (if
supported and allowed by the OS). This uses an algorithm that has to
guess the timezone. This is by no means going to work correctly for
every situation. A warning is shown when this stage is reached. Do not
rely on it to be guessed correctly, and set date.timezone to the
correct timezone instead.

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().

get server time in php - timezone issues

on shell, my server time is (simple date in bash):
Sun Aug 29 10:37:12 EDT 2010
when I run the code php -r "echo date('Y-m-d H:i:s');" I get: 2010-08-29 10:37:10
but when I execute a php script that has echo date('Y-m-d H:i:s'); I get a different time- since php thinks the timezone is UTC (server time is EDT).
My php.ini file has no timezone set, and I wouldnt like to change anything- as I have no idea what affect it would have on other sites that run on this server.
Is there a simple way to get the server time- at the timezone that is set for the server?
According to the php.ini directives manual page, the timezone isn't set by default if you're using a version prior to 5.3. Then if you take a look at the page for date_default_timezone_get, it uses the following order for getting the timezone:
* Reading the timezone set using the date_default_timezone_set() function (if any)
* Reading the TZ environment variable (if non empty) (Prior to PHP 5.3.0)
* Reading the value of the date.timezone ini option (if set)
* Querying the host operating system (if supported and allowed by the OS)
UTC is the default if all of those fail, so that's probably a good starting point.
The server should always have a timezone specified in the php.ini file. PHP 5.3 even raises notices if that's not the case and the DateTime extension will throw exceptions if you attempt to build objects without explicitly specifying a timezone (with a DateTimezone object) or having a default one (either through date.timezone or date_default_timezone_set).
By what you're describing, the most likely scenarios are:
Different php.ini files are being used in the two scenarios. In the first case, no default timezone is specified, so PHP attempts to infer one from the system-wide timezone of the machine.
The (second) script calls date_default_timezone_set('UTC') or equivalent.
Late for the party, but if you want to set the timezone without edit php.ini, you can put in the php code one of these strings:
date_default_timezone_set('GMT');
date_default_timezone_set('Europe/Zurich');
date_default_timezone_set('America/New_York');
Have you tried using date_default_timezone_set()
American timezones can be found here http://www.php.net/manual/en/timezones.america.php

Categories