PHP timestamps & timezone configuration - php

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

Related

Convert PHP Mongo UTC Date From Milliseconds To Date for TimeZone

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

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)) ;

How can I covert milliseconds (unix timestmap) to utc php

I am creating web services for android in php, they are sending time in unix timestamp (milliseconds).
Now I need to convert this in utc timestamp and compare with mysql created_at.
I have tried that:
$time = 1443001794;
$seconds = $time / 1000;
echo date("d-m-Y", $seconds);
But it always returns '17-01-1970'.
Well if you only want to compare with mysql timestamp then you can do like this:
$result = strtotime($mysqlCreateAt);
strtotime will convert your timestamp in unix timestamp, and then you can compare with any time you want.
If you want to convert this to utc, try this:
$date = new DateTime();
$date->setTimestamp(1443001794);
var_dump(gmdate('Y-m-d H:i:s', strtotime($date->format('Y-m-d H:i:s'))));
It will give to UTC timestamp
MYSQL date format is "yyyy-mm-dd":
So simply use this:
$time = 1443001794;
echo date("Y-m-d", $time);
If You want date with time . Then Try this
$time = 1443001794;
echo date("Y-m-d H:i:s", $time);
If you want to compare then you can just use strtotime for that.
$db_time = strtotime($created_at); // it will convert your db created at in unix
if($unixtime > $db_time){
// your code here
}

how to convert my TIMESTAMP to UTC timestamp in magento

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.

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