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
Related
I have a date in UTC / Epoch format from a mongo database (1659052800000) and I'm trying to convert it to a friendly format using PHP for the PST timezone.
Here's what I've tried.
date_default_timezone_set('PST');
$this->vars[dateOfEvent] = date('Y-m-d', 1659052800000);
and
date_default_timezone_set('PST');
$dt = new DateTime(1659052800000);
$this->vars[dateOfEvent] = $dt->format('Y-m-d');
but neither give me the correct date. I'm expecting it to out "2022-07-28"
You can set the timezone temporarily for the DateTime object. Also you need to set timestamp divided by 1000 or remove the last three zeroes.
$dt = new DateTime();
$dt->setTimestamp(1659052800000 / 1000);
$dt->setTimezone(new DateTimeZone('PST'));
echo $dt->format('Y-m-d');
prints
2022-07-28
I want to convert date UTC for Europe/Lisbon, but the code I have gives me different outputs/times:
$datafull = "13-04-2021 08:47:13";
$date = new DateTime($datafull);
$date->setTimezone(new DateTimeZone('Europe/Lisbon'));
echo $date->format('d-m-Y H:i:s (e)');
// 13-04-2021 09:47:13 (Europe/Lisbon)
$datetime = new DateTime($datafull, new DateTimeZone('Europe/Lisbon'));
print $datetime->format('d-m-Y H:i:s (e)');
// 13-04-2021 08:47:13 (Europe/Lisbon)
When you supply a timezone object to the DateTime constructor you're telling it in what timezone the give $datafull is. So in:
$datetime = new DateTime($datafull, new DateTimeZone('Europe/Lisbon'));
You say it is in Europe/Lisbon, and it stays there.
In the other code:
$date = new DateTime($datafull);
$date->setTimezone(new DateTimeZone('Europe/Lisbon'));
The default timezone is used when the DateTime is constructed, probably UTC on your server, and then you change it afterwards on the second line to Europe/Lisbon, which is an hour ahead.
See: DateTime::__construct
My plan:
Get the current timestamp using strtotime("now")
Convert that to timezone '0' - This is the part I don't know how to do. I have the number that represents the users timezone, like -8 hours for example.
Store it in the database in timezone '0'
Retrieve it from the database in timezone '0'
Convert it to the users timezone in the opposite direction
use the date('', timestamp) function to display it
How can I accomplish the conversion? Or am I going about this wrong?
I need to be able to store it in the database as a numerically represented time (like strtotime returns)
Using time() is the same as strtotime("now") and you do not need to worry about converting the timezone of the timestamp, as the timestamp has no timezone:
Does PHP time() return a GMT/UTC Timestamp?
time returns a UNIX timestamp, which is timezone independent. Since
a UNIX timestamp denotes the seconds since 1970 UTC you could say it's
UTC, but it really has no timezone.
You can then store that timestamp in your database. When you retrieve it you can convert it to the users timezone. With something like this:
date_default_timezone_set('UTC');
$timestamp = '1429066967';
//Supported Timezones: http://php.net/manual/en/timezones.php
$userTimezone = 'America/Los_Angeles';
$dt = new DateTime();
// Set the timestamp
$dt->setTimestamp($timestamp);
// Set the timezone
$dt->setTimezone(new DateTimeZone($userTimezone));
// Format the date
$date = $dt->format('Y-m-d H:i:s');
echo $date;
Outputs: 2015-04-14 20:02:47
But if you only have the UTC offset you could try this:
date_default_timezone_set('UTC');
$timestamp = '1429066967';
$offset = -8;
$userTimezone = timezone_name_from_abbr("", $offset*3600, false);
$dt = new DateTime();
// Set the timestamp
$dt->setTimestamp($timestamp);
// Set the timezone
$dt->setTimezone(new DateTimeZone($userTimezone));
// Format the date
$date = $dt->format('Y-m-d H:i:s');
echo $date;
Which also outputs: 2015-04-14 20:02:47
i found many articals on this but none of give proper soluation
my current system timezone is = 'asia/Kolkata'
my current magento timezone is 'asia/shanghai'
this give me de correct UTC time here system time is '2013-06-26 15:34:00'
$timezone = date_default_timezone_get();
date_default_timezone_set($timezone);
$now = Mage::getModel('core/date')->timestamp(time());
$current_date= date('Y-m-d H:i:s');
so here $current_date = '2013-06-26 18:04:30'
but i have another mytimestamp..... ex :- $time = '2013-06-26 15:34:00';
$scheduletime = '2013-06-26 15:19:00';
$dateTimestamp = Mage::getModel('core/date')->timestamp(strtotime($scheduletime));
$schedule_time = date('Y-m-d H:i:s',strtotime($scheduletime));
so here $schedule_time = '2013-06-26 23:34:00'
so actually what i want is to get difference between this two time and it should be : 0
Try
Mage::getSingleton('core/date')->gmtDate();
Note: UTC and GMT are equal.
Use strtotime to generate a timestamp from the given string (interpreted as local time) and use gmdate to get it as a formatted UTC date back.
For example
As requested, here’s a simple example:
echo gmdate('d.m.Y H:i', strtotime('2012-06-28 23:55'));
or you can also use local setting with store like below
Mage::app()->getLocale()->storeDate(
$this->getStore(),
Varien_Date::toTimestamp($this->getCreatedAt()),
true
);
where $this->getCreatedAt() your Database date.
I am trying to convert time between current time to UTC and UTC to current time zone.
Here is what I have done:
$schedule_date = new DateTime($triggerOn, new DateTimeZone('UTC') );
$triggerOn = $schedule_date->format('Y-m-d H:i:s');
echo $triggerOn;
The output value does not change the only thing that changes in format.
the string $triggerOn was generated based on America/Los_Angeles timezone
This is how my string looks like before and after:
BEFORE 04/01/2013 03:08 PM
AFTER 2013-04-01 15:08:00
So the issue here is that DateTime does not convert to UTC.
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
You are consuming the date/time and setting the time zone correctly, however before formatting the datetime, you are not setting the desired output timezone. Here is an example which accepts a UTC time zone, and converts the date/time to the America/Los_Angeles time zone:
<?php
$original_datetime = '04/01/2013 03:08 PM';
$original_timezone = new DateTimeZone('UTC');
// Instantiate the DateTime object, setting it's date, time and time zone.
$datetime = new DateTime($original_datetime, $original_timezone);
// Set the DateTime object's time zone to convert the time appropriately.
$target_timezone = new DateTimeZone('America/Los_Angeles');
$datetime->setTimeZone($target_timezone);
// Outputs a date/time string based on the time zone you've set on the object.
$triggerOn = $datetime->format('Y-m-d H:i:s');
// Print the date/time string.
print $triggerOn; // 2013-04-01 08:08:00
Create the date using the local timezone, then call DateTime::setTimeZone() to change it.