default time zone is not changing using php or php.ini - php

i'm trying to change the default time zone using php but not working
date_default_timezone_set('Asia/Riyadh')
and tried using php.ini
date.timezone="Asia/Riyadh"
but in phpinfo i get this
Default timezone Europe/Berlin
date.timezone Europe/Berlin Europe/Berlin
and when inserting time to MySQL using CURTIME() i don't get Asia/Riyadh time and get a wrong time
update (timezone changed after awhile but using CURTIME() to insert date into mysql doesn't enter the right time

in php.ini
date.timezone="Asia/Riyadh"
and in my script
$nowtime = date("G:i:s");
$nowdate = date("Y-m-d");

Yes, you must always restart your server when making any changes to your php.ini file.
Another thing to look into if you are positive that you are updating the correct .ini file is to look at the "Additional .ini files parsed" in your phpinfo(). Sometimes you will find that certain programs or updates may add additional files here and it will override your php.ini settings.

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 datetime doesn't match with my windows system

I used date("Y-m-d H:i:s"); to get current date time format but the result doesn't match with my Windows system.
Example:
<?php
echo "Current date time: ".date("Y-m-d H:i:s")."<br />";
?>
The sample code displaying result:
Current date time: 2012-05-30 01:58:21
But my Windows system date time is 2012-05-30 09:56:04
I have tried using date_default_timezone_set('Asia/Singapore') function to get my region datetime and it's work but I want it set default in my php.ini.
For example: (it's work)
<?php
date_default_timezone_set('Asia/Singapore');
echo "Current date time: ".date("Y-m-d H:i:s")."<br />";
?>
I have also tried to set default value in php.ini that is date.timezone = "" changed to date.timezone = "Asia/Singapore" but doesn't work.
I use Windows 7 Home Premium 64bit and php-5.3.13-Win32-VC9-x86. Does anyone know how to match between these value?
date_default_timezone_set will supersede php default timezone setting, without needing to restart anything.
But in your case it did not work. that's weird. Try this as this is my last resort:
<?php
header('Content-Type: text/plain');
//date_default_timezone_set('Asia/Singapore');
date_default_timezone_set('Etc/GMT+8');
echo date("Y-m-d H:i:s A T", time());
?>
UPDATES:
Since what you need is a default php timezone and permanent solution without using date_default_timezone_set, following the few simple steps below:
Create a file called phpinfo.php with one line content <?php echo phpinfo(); ?>
Run http:// localhost /phpinfo.php and look for Loaded Configuration File. Example value C:\wamp\bin\apache\Apache2.2\bin\php.ini
Open the above php.ini file with a notepad. Look for the [Date] and modify as below:
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Singapore
Save it and restart your apache. Run again your datetime.php. Or you can also verify directly from phpinfo.php under a h1 header date by making sure the default timezone is read as "Asia/Singapore" like below:
Are you running that script via webserver (e.g., Apache) or CLI? Apache needs to be restarted after making changes to php.ini.
Are you sure that you are editing the correct php.ini file? Create a phpinfo() page and verify the path to the php.ini file in the output.
You need to define the default Timezone
date_default_timezone_set('Asia/Singapore');
UPDATE:
Have you restarted server after change PHP ini?

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

Wrong PHP date() output in wamp server

The problem is that date('r') returns wrong time for my timezone.
php.ini timezone setting:
date.timezone = Europe/Kiev
date_default_timezone_set('Europe/Kiev') in my script solves the problem.
So what's wrong with WAMP?
I suggest always using date_default_timezone_set() from script
e.g.
date_default_timezone_set('Europe/Kiev');
or
ini_set('date.timezone', 'Europe/Kiev');
...to avoid PHP guessing timezone.
It comes handy when you transfer code to different server(s), for example, outside of Ukraine. This line should help you to avoid unexpected (wrong) results if date.timezone is not set in php.ini or its setting is incorrect. It's also handy when you can't access and/or modify php.ini (shared hosting).
Also, be sure that you've not used ; at the and of line in php.ini.
Restart server after changing php.ini.
Edit php.ini and restart Apache:
left click to WampServer in tray icon
open php.ini (go to PHP -> php.ini)
set new date.timezone value
;date.timezone = UTC
date.timezone = Europe/Kiev
restart Apache ( go to Apache -> Service -> Restart Service )
check value of date.timezone by phpinfo();
You need to reload the configuration / restart the server after editing your php.ini file.
According to the documentation of date_default_timezone_get, the date.timezone configuration option can be overridden by setting the TZ environement variable (which, in turn, can be overridden by calling date_default_timezone_set). From your description, I suspect that the TZ environement veriable is set.
by default it shows GMT time you can change for your region with following code
date_default_timezone_set("Asia/Bangkok");//set you countary name from below timezone list
echo $date = date("Y-m-d H:i:s", time());//now it will show "Asia/Bangkok" or your date time
List of Supported Timezones
http://www.php.net/manual/en/timezones.php
I know this is an old question.
If you are using a PHP framework, you might want to check the config file of the framework. For example in Laravel, open the config/app.php and you will find timezone there. Set it to your timezone.
Open your php.ini with maybe notepad++, sublime text...
Add this line to the file: date.timezone = "X" Where X is the your time-zone of wish. Get a list of supported timezones here: http://php.net/manual/en/timezones.php
... That should do it.
3. Restart your server

Handling timezones in PHP and MySQL?

I'm testing out making an entry system but the problem is, my host's timezone is different from where I live. It's 3am on my server while on my local computer, the time is 4pm. How do I automatically adjust code that I pull from the database to be displayed as a timezone similar to where I'm at?
You can set the default timezone using the function date_default_timezone_set(). It sets the default timezone used by all date/time functions in a script.
date_default_timezone_set('America/Los_Angeles');
Have a look at this page:
http://php.net/manual/en/function.date-default-timezone-set.php
You can find a list of timezones here
http://php.net/manual/en/timezones.php
Alternatively you could change the php.ini file if you have the permissions for it. You could change the option date.timezone to your timezone like this:
date.timezone = "US/Central"
For more information about date.timezone, go to this website:
http://kb.siteground.com/article/How_to_change_the_datetimezone_value_in_PHP.html

Categories