Convert PHP Mongo UTC Date From Milliseconds To Date for TimeZone - php

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

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

Convert datetime UTC to local time in PHP

I have two pieces of data being returned in JSON:
"startTimeUTC":"2018-07-03T21:00:00.000Z"
"siteTimeZone":-4.0"
I am then doing this in PHP:
$start = date_format(date_create($event['startTimeUTC']), 'Y-m-d h:i:s');
Which returns this:
2018-07-03 09:00:00
I am not sure how I subtract the timezone offset from the original time.
The expected time should be: 05:00:00 PM
I have tried so many different things to get this to work, but have had no luck.
How do I get the correct time to convert to the local time based on the timezone offset?
The date string already contains the Zulu timezone. So you need to create a new DateTime object, and then set the new timezone afterwards.
$date = new Datetime('2018-07-03T21:00:00.000Z');
$date->setTimezone(new DateTimeZone('-4.0'));
var_dump($date->format('Y-m-d H:i:s')); // 2018-07-03 17:00:00
You can use the timezone as a multiplier to add/subtract from the UTC time.
// convert your time via `strtotime` and then add your offset * 3600 (seconds in an hour)
$start = date('Y-m-d h:i:s',strtotime($event['startTimeUTC']) + ($event['siteTimeZone'] * 3600)) ;

PHP timestamps & timezone configuration

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

Get current UTC string in PHP

Is there a way in PHP to calculate the current UTC string? Sometihng that would throw current UTC timestring format on each different server.
In europer it would throw "UTC +1" ... or "UTC 0" ... , "UTC +12:00" always depending on current server settings.
Return UTC timestring on different servers:
$dt = new DateTime('now', new DateTimezone('UTC'));
# or
$dt = new DateTime('UTC');
echo $dt->format('Y-m-d H:i:s');
If you have DateTime objects in different timezones, you can always get timezone offset by calling DateTime::getOffset() who will return offset in seconds; or just use format you need:
$dt = new DateTime('now', new DateTimeZone('America/New_York'));
echo $dt->getOffset(), " : ", $dt->format('O / P / e / T / Z');

How to convert between time zones in PHP using the DateTime class?

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.

Categories