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

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

Related

What is this date format? 2021-10-04T08:19:54.000+04:00 [duplicate]

This question already has answers here:
What time stamp format is this?
(2 answers)
Closed 1 year ago.
And how can I covert it to 'd.m.Y H:i:s' with php?
gmdate('d.m.Y H:i:s', '2021-10-04T08:19:54.000+04:00')
did not help
The date format is ISO8601 if I'm not mistaken. PHP can parse this using the default DateTime class:
$date = new DateTime('2021-10-04T08:19:54.000+04:00');
$date->format('d.m.Y H:i:s');
For this purpose just use the native DateTime class. It can interpret several formats. Yours looks like ISO8601.
echo (new DateTime('2021-10-04T08:19:54.000+04:00'))->format('d.m.Y H:i:s');
its ISO 8601 date and you can format this easyly with dateTime or carbon.
You can find more info here https://www.php.net/manual/en/datetime.format.php

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

Get the current date and time in PHP with '2016-07-04 00:00:00.000' format [duplicate]

This question already has answers here:
How to get GMT date in yyyy-mm-dd hh:mm:ss in PHP
(6 answers)
Closed 6 years ago.
I am using mssql db with php. Which PHP function can return the current datetime. (i.e.) I want the current date and time to be saved in the following format say for example,
2016-07-04 11:10:05.000
Thanks!
Use date->format http://php.net/manual/it/function.date.php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s.u');
12-hour format
date("Y-m-d h:i:s.u")
24-hour format
date("Y-m-d H:i:s.u")
With "date" you can format the output and with "time" you get the current unix time.
Example
echo date("Y-m-d H:i:s.u", time());
Documentation: http://php.net/manual/it/function.date.php

convert DateTime to another time zone [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have a time in 4:40 pm et, how to convert it in H:i:s, I have used this :
date ('H:i:s' ,strtotime('4:40 pm et'));
You just need to use see timezone and datetime
<?php
$old = date ('H:i:s' ,strtotime('4:40 pm et'));
$date = new DateTime($old);
$newdate = $date->setTimezone(new DateTimeZone('Europe/Moscow')); // +0
print_r($newdate);

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