PHP - change timezone display for whole site - php

I have a website which displays dates from database using date() function. Now I have to add support for various timezones. Time needs to be changed only for display purposes in the front end. I used date_default_timezone_set('Europe/London'), but it did not affect the date output - it stays the same as in the database. Function date_default_timezone_get() shows that the timezone is set successfully, but the date output stays the same. How can I change the global output of date? Can it be done without editing all the date() functions in the site?
Full example:
date_default_timezone_set('Europe/London');
echo date_default_timezone_get(); //shows Europe/London
$date_from_db = '2014-02-02 12:34:05'; //this is generally taken from database
echo date('Y-m-d H:i:s',strtotime($date_from_db));
Outputs the same as before.

If your dates are stored in GMT, you can let strtotime() know by appending the timezone:
echo date('Y-m-d H:i:s', strtotime("$date_from_db GMT"));
Of course, setting Europe/London as the timezone, the results are likely the same.
Or, in your case:
echo date('Y-m-d H:i:s', strtotime("$date_from_db +0100"));
// 2014-02-02 11:34:05

Try to use DateTime class - see examples on this page http://www.php.net/manual/en/datetime.settimezone.php
<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>

Related

Parse a UTC date, output local datetime

I have already read How to get local time in php? and similar questions, but here I want to do the contrary:
With a given datetime in UTC (e.g. 2021-03-31 23:45:00), how to output the date in local timezone?
$dt = new DateTime("2021-03-31 23:45:00"); // parse the UTC datetime
echo $dt->format('m/d/Y, H:i:s');
In Europe/Paris timezone, it should output 2021-04-01 01:45:00, but here it sill outputs 2021-03-31 23:45:00. How to fix this?
I also tried with strtotime with a similar result; I also tried with:
date_default_timezone_set('UTC');
$dt = new DateTime("2021-03-31 23:46:14");
date_default_timezone_set('Europe/Paris');
echo $dt->format('m/d/Y, H:i:s');
without success.
You need to change the timezone of the date (using DateTime::setTimeZone()), not the default timezone:
date_default_timezone_set('UTC');
$dt = new DateTime("2021-03-31 23:46:14");
$dt->setTimeZone(new DateTimeZone("Europe/paris")); // change date timezone
echo $dt->format('m/d/Y, H:i:s');
Output:
04/01/2021, 01:46:14
Changing the default timezone affects the new DateTime(), not the format() result.
This can also be easily solved with date and strtotime:
//The following line is only required if the server has a different time zone.
//date_default_timezone_set('Europe/Paris');
$utcDate = "2021-03-31 23:45:00";
echo date('m/d/Y, H:i:s',strtotime($utcDate.' UTC'));
Output:
04/01/2021, 01:45:00

how to get the correct time with php

i am trying to get the correct time with php
not the one that the user's computer gives is there a function that gives the correct "GMT" time as an example .
PHP gets the server date, not the user computer date.
If you need to get a different date timezone then you need set the timezone to your desired.
See: http://php.net/manual/en/function.date.php
like:
$tz = 'Europe/London';
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
echo $dt->format('d.m.Y, H:i:s');
If you just want GMT time, you can use gmdate instead of date. For example,
echo date('Y-m-d H:i:s') . "\n";
echo gmdate('Y-m-d H:i:s') . "\n";
Output (for a server in Europe)
2019-02-04 03:34:05
2019-02-04 02:34:05
Demo on 3v4l.org
You will be getting the server time only, not the computer time
$now = new DateTime(null, new DateTimeZone('America/New_York')); //timezone format
echo $now->format("Y-m-d\TH:i:sO"); // Server time

strange timezone issue EDT/EST

I have checked my mysql system timezone with SELECT ##system_time_zone; and its return EDT in production server. Now I want to generate same timezone's (i.e EDT) current timestamp which is not working. This is what i have tried so far is
date_default_timezone_set('EST');
$ts = strtotime('now');
echo date('Y-m-d H:i:s',$ts);
and
$date = new DateTime(null, new DateTimeZone('EST'));
$ts = $date->getTimestamp();
echo date('Y-m-d H:i:s',$ts);
its output is 2015-09-07 05:38:43 but my mysql date (timestamp) is 2015-09-07 06:39:53 so its not same. (tested by inserting same time)
So how can I get current timestamp based on mysql's system time zone (EDT/EST)?? any help from proessional appricated
The current values of the global and client-specific time zones can be retrieved like this:
mysql> SELECT ##global.time_zone, ##session.time_zone;
And try to use this code to set time zone and Location of time zone
<?php
$date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
date_timezone_set($date, timezone_open('Pacific/Chatham'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
?
Note second php code is just an example.
I hope I helped you

Convert timezone in php

I have these timezones. I want to get the current datetime depends on the given timezone. The user will select either one timezone. so need to return current time.
ASKT
CDT
EDT
HST
MDT
MST
PDT
How can i convert? Please help
DateTime::setTimezone would help you.
<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
Use the DateTime Class
$time = time(); //Get the current time
$date = new DateTime($time, new DateTimeZone('Pacific/Nauru')); //Set a time zone
echo $date->format('Y-m-d H:i:sP') . "\n"; //display date
$date->setTimezone(new DateTimeZone('Europe/London')); //set another timezone
echo $date->format('Y-m-d H:i:sP') . "\n"; //display data again
This, way you don't have to give the same timestamp as new argument every time like mishu's answer.
See the class written by "the_dark_lord12001 at yahoo dot com" this will help you to get timezones from abbreviations & then you can use it
with either date_default_timezone_set or DateTime class
http://www.php.net/manual/en/datetimezone.listabbreviations.php
Hope this help.
~K

php DateTime object with unix timestamp wrong - bug?

Just stumbled upon this weird bug with php's DateTime object...
Check this out:
<?php
$date = 1335823200;
echo date('d',$date);
echo '<br />';
$date = new DateTime("#$date");
echo $date->format('d');
?>
Returns:
06
05
It doesn't happen with any timestamp. I suspect that it has something to do with different timezones, but playing around with setlocale() didn't help anything. By the way, the '#' in the DateTime is needed to be able to use unix timestamps (see bug report here). Here a few more timestamps to test:
1333663200
1338588000
1338847200
Since you did not specify timezone for DateTime it is supposed that it is UTC, while date respects current timezone (specified by date_default_timezone_set or taken from php.ini). Just execute this and see:
$date = 1335823200;
echo date('d-m-Y H:i:s',$date);
echo '<br />';
$date = new DateTime("#$date");
echo $date->format('d-m-Y H:i:s');

Categories