Convert time and date from one time zone to another in 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";
?>
The above examples will output:
2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45
found on DateTime Manual on php.net
EDIT:
Like Pekka said: The DateTime class exists from 5.2 on and there you first have to find out which of the methods are realy implemented and which one only exist from 5.3 on.
How to convert time from one timezone to another in PHP
date_default_timezone_set('Europe/London');
$datetime = new DateTime('2008-08-03 12:35:23');
echo $datetime->format('Y-m-d H:i:s') . "\n";
$la_time = new DateTimeZone('America/Los_Angeles');
$datetime->setTimezone($la_time);
echo $datetime->format('Y-m-d H:i:s');
converting a time to a different timezone with php
Use the date_default_timezone_set() function of PHP.
If you want to change it to France you would use the
date_default_timezone_set('Europe/Paris');
a list of Supported Timezones can be found here:
http://www.php.net/manual/en/timezones.php
The functionality of date_default_timezone_set() can be found here:
http://php.net/manual/en/function.date-default-timezone-set.php
How to convert datetime from one time to another by timezone abbreviation in PHP
Try this:
function convertDateTimes($dateTime, $fromTz, $toTz, $format = 'Y-m-d H:i:s') {
$fromTz = new DateTimeZone($fromTz);
$dateTime = new DateTime($dateTime, $fromTz);
$toTz = new DateTimeZone($toTz);
$dateTime->setTimeZone($toTz);
return $dateTime->format($format);
}
Sample usage:
$dateTime = '2018-04-13 09:19:53';
$fromTz = 'EDT';
$toTz = 'PDT';
echo "Input: $dateTime EDT <br>";
echo 'Output: ', convertDateTimes($dateTime, $fromTz, $toTz), " $toTz";
Output:
Input: 2018-04-13 09:19:53 EDT
Output: 2018-04-13 06:19:53 PDT
Timezone conversion in php
You can use the datetime object or their function aliases for this:
Example (abridged from PHP Manual)
date_default_timezone_set('Europe/London');
$datetime = new DateTime('2008-08-03 12:35:23');
echo $datetime->format('Y-m-d H:i:s') . "\n";
$la_time = new DateTimeZone('America/Los_Angeles');
$datetime->setTimezone($la_time);
echo $datetime->format('Y-m-d H:i:s');
Edit regarding comments
but i cannt use this method because i need to show date in different time zones as the user login from different locations
That's not a problem. When a user logs in, you determine his timezone and set it to your DateTime object just like shown. I'm using a similar approach in one of my projects and it works like a charm.
in the database i need to get the dates in any single timezone, then only it can be processed properly
You store the time either as a timestamp or a datetime in one timezone. When you query a DateTime field, you either convert the time in a DateTime object to this timezone or - if your db supports it - query with the selected timezone.
Converting between timezones in PHP
I would not use date_default_timezone_set
for general TZ conversions. (To clarify... if this is for display purposes, script wide, then using the default timezone is a reasonable thing to do.)
Instead, I would use something like:
$tz = new DateTimeZone('America/Los_Angeles');
$date = new DateTime('Thu, 31 Mar 2011 02:05:59 GMT');
$date->setTimezone($tz);
echo $date->format('l F j Y g:i:s A I')."\n";
Retrieve date and convert it to specific time zone according to user time zone
$date = new DateTime($result->s_start, new DateTimeZone($result->s_timezone));
$date->setTimezone(new DateTimeZone('Africa/Cairo'));
echo $date->format('Y-m-d H:i:sP') ;
Resources
- DateTime class
- DateTimeZone class
Edit A function in the controller to convert timezones.
public function _convert_time($result){
$date = new DateTime($result->s_start, new DateTimeZone($result->s_timezone));
$date->setTimezone(new DateTimeZone('Africa/Cairo'));
return $date->format('Y-m-d H:i:sP') ;
}
Now you can echo the result
foreach($results as $result){
echo $this->_convert_time($result);
}
How to convert between time zones in PHP using the DateTime class?
What you're looking for is this:
$triggerOn = '04/01/2013 03:08 PM';
$user_tz = 'America/Los_Angeles';
echo $triggerOn; // echoes 04/01/2013 03:08 PM
$schedule_date = new DateTime($triggerOn, new DateTimeZone($user_tz) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$triggerOn = $schedule_date->format('Y-m-d H:i:s');
echo $triggerOn; // echoes 2013-04-01 22:08:00
How to convert UTC datetime into PST datetime in php
Like this.
$utc_date = '2020-07-31T00:00:00.000Z';
$jsDateTS = strtotime($utc_date);
if ($jsDateTS !== false)
echo date('Y-m-d H:i:s', $jsDateTS );
Edit: Changed code to include timezone change.
$utc_date = '2020-07-31T00:00:00.000Z';
$timestamp = strtotime($utc_date);
$date = new DateTime();
$date->setTimestamp($timestamp);
$date->setTimezone(new \DateTimeZone('America/Los_Angeles'));
echo $date->format('Y-m-d H:i:s') . "\n";
Working Example.
Related Topics
How to Use Arrays in Curl Post Requests
How to Make Dot Match Newline Characters Using Regular Expressions
How to Make This Preg_Match Case Insensitive
Sort and Display Directory List Alphabetically Using Opendir() in PHP
Detect Exact Os Version from Browser
PHP, MySQL Error: Column Count Doesn't Match Value Count At Row 1
Use One Bind_Param() With Variable Number of Input Vars
MySQL_Fetch_Array, MySQL_Fetch_Assoc, MySQL_Fetch_Object
How to Use MySQLi_Fetch_Array() Twice
Get Difference Between Associative Rows of Two 2-Dimensional Arrays
Shortcomings of MySQL_Real_Escape_String
How to Get the Sqlsrv Extension to Work With PHP, Since Mssql Is Deprecated
Set Cookie Wih Js, Read With PHP Problem
PHP Pdo Prepared Statement - MySQL Like Query
Preserve Key Order (Stable Sort) When Sorting With PHP'S Uasort