Handling timezones in PHP and MySQL? - php

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

Related

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

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.

Where does PHP get it's DATE information from?

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

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 date() in offline WAMP differs from system time

why does the PHP date() in my offline WAMP different from my system time? how can i sync it?
You should set the default timezone. You can do this in php.ini, just add the following line:
date.timezone ="Europe/Stockholm"
You should obviously change Europe/Stockholm to your timezone. If you don't have access to php.ini, you can use date_default_timezone_set and pass your timezone as a parameter, like:
date_default_timezone_set("Europe/Stockholm");
Here is a list of supported timezones.

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