convert DateTime to another time zone [duplicate] - php

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

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 would I change one date format to another in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
How would I change the date 18/08/2018 10:46:15 pm to Sat 25, 10:46:15 pm in PHP. I am doing the following:
```
$OldDate = "18/08/2018 10:46:15 pm";
$NewDate = $OldDate->format(D-j, Y-H:I:S);
echo $NewDate;
?>```
However, it is not showing anything, am I formatting it correctly?
Your old date is just a string. If you want to format it, you need to create a DateTime object using that string the constructor.
You're also incorrectly specifying the format
$OldDate = new DateTime("18/08/2018 10:46:15 pm");
echo $OldDate->format("L-j, h:i:s A");
http://php.net/manual/en/class.datetime.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

DateTime from unix timestamp is wrong [duplicate]

This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Closed 9 years ago.
I have a string "2013-10-09 00:00:00" and I use the code below to change it to a timestamp
date_default_timezone_set($timeZone);
$timeStamp = strtotime("2013-10-09 00:00:00"); //echos 1381269600
When I do
date_default_timezone_set($timeZone);
date("Y-m-d H:m",$timeStamp);
I get 2013-10-09 00:10:00. This is completely strange. Why do I get this 10 minutes difference?
Because you are using the m which is for the month not for the minute. You need to use i.
See http://php.net/manual/en/function.date.php
Your code should be
date("Y-m-d H:i", $timeStamp);

Categories