Convert TIMESTAMP and timezone to a different timezone [duplicate] - php

This question already has answers here:
Timezone conversion in php
(9 answers)
Closed 6 years ago.
So I have in the DB a timestamp like this: 2016-03-31 21:10:15 this get's set in db to the server timezonedate_default_timezone_set("America/Los_Angeles");
Now when I list the data I need to convert this to a user timezone which I have stored somewhere else, is there a way to convert this and display it in a different timezone?
So if it was 5:00 PM for "America/Los_Angeles" I need to convert this to New_York for eg and show that time but for New_York timezone, I guess you know what I mean.

Use DateTime objects, and you can play around with timezones to your heart's content:
$dateTime = '2016-03-31 21:10:15';
$originalTimezone = 'America/Los_Angeles';
$newTimezone = 'America/New_York';
$dto = new DateTime($dateTime, new DateTimeZone($originalTimezone));
$dto->setTimezone(new DateTimeZone($newTimezone));
echo $dto->format('Y-m-d H:i:s');

Related

How can I convert time to UTC in PHP? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 2 years ago.
I am getting time with timezone from an API.
Example: 2019-06-25T08:01:32-05:00
How can I change to UTC time using PHP?
You can use the DateTime and DateTimeZone classes:
// this date format will consider the timezone '-05:00'
$dt = new DateTime('2019-06-25T08:01:32-05:00');
// then you convert it to utc
$dt->setTimeZone(new DateTimeZone('utc'));
echo $dt->format('Y-m-d H:i:s'); // will give you 2019-06-25 13:01:32
Manual DateTime: https://www.php.net/manual/en/class.datetime.php
Manual DateTimeZone: https://www.php.net/manual/en/class.datetimezone.php

How do I get my local timezone name in php? [duplicate]

This question already has answers here:
Get name of time zone
(14 answers)
Closed 2 years ago.
I need my local timezone name like America/Chicago ?
I have written my code like this
$date = new DateTime();
$timeZone = $date->getTimezone();
echo $timeZone->getName();
But I am getting UTC as output but I don't need this. I need the timeZone name. How can I get it ?
You are probably getting the timezone set in the server, that's the best you can get. If you want America/Chicago you will need to configure that in the server.
You could also specify the timezone you want to work with when instantiating a new DateTime object, but since the server is probably set as UTC, you need to be careful and juggle the conversion. For instance:
$dt = new DateTime('2014-06-22 11:10:58', new DateTimeZone('UTC'));
$dt->setTimezone(new DateTimeZone("Europe/Paris"));
$dateString = $dt->format('Y-m-d h:i:s');

how to get local date from timestamp = 1574463600 in php mysql [duplicate]

This question already has answers here:
Convert timestamp to readable date/time PHP
(14 answers)
Closed 3 years ago.
i am using sports api and i am getting matches schedule from api but the timestamp is UTC and i want to search match according to my localdate the timestamp is look like this 1574463600
You can use # along with the unix time to create a DateTime class.
<?php
$unixTime = 1574463600;
$date = new DateTime('#' . $unixTime);
echo $date->format('d/m/Y'); // outputs 22/11/2019
See it here https://3v4l.org/eoSU9
Read about DateTime here https://www.php.net/manual/en/class.datetime

How to convert SQL datetime date format into unix utc format? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have standart sql datetime format: 2016-10-24 14:26:53.000
And i dont know how to convert into UTC format should be something like: 138853800000
Can be done in sql or php.
I tried something like:
$d = DateTime::createFromFormat('Y-m-d', 2016-10-24, new DateTimeZone('UTC'));
echo $d->getTimestamp().'<br>';
I works but after i add hours minutes it stops
Here is my two cents :
$d = new dateTime("2016-10-24 14:26:53.000", new DateTimeZone("UTC"));
echo $d->getTimestamp();
Output :
1477319213
echo $d->format('Y-m-d H:i:s.u P');
output :
2016-10-24 14:26:53.000000 +00:00

How to convert timestamp to actual Date and Time with GMT timezone [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a timestamp which is like this
$timestamp = time();
so the $timestamp has 10 digit timestamp value...
now i use the follwoing code to convert it into (YYYY-mm-dd H:i) format by the following method
$act_time = date('Y-m-d H:i', $arr_timestamp);
But now i want to convert it based on the GMT timezone format..
How can i achieve that?
NOTE: I dont want to use php's DateTime object
You can use date_default_timezone_set():
date_default_timezone_set("GMT");
List of Supported Timezones.
You can save the current timezone before changing it:
$backupTimezone = date_default_timezone_get();
In addition, instead of using these you can change the default timezone in your ini file: date.timezone

Categories