13

I want to convert date form from d/m/Y to Y-m-d with timezone offset. I am able to convert from d/m/Y to Y-m-d with this code:

$date = DateTime::createFromFormat('d/m/Y', $date);
$date = $date->format('Y-m-d');

But I am not sure how to add the timezone offset.

3 Answers 3

34

(PHP 5 >= 5.3.0) you actually enter the third parameter

public static DateTime DateTime::createFromFormat(string $format , string $time[, DateTimeZone $timezone])

$date = DateTime::createFromFormat('d/m/Y', $date, new DateTimeZone('Europe/Berlin'));
Sign up to request clarification or add additional context in comments.

5 Comments

I don't know why this answer is not selected as solution. This is correct answer.
It does not work for me.. It seems like third parameter (DateTimeZone Object) was simply ignored. The only way to achieve this was to use the validated answer published by @John Conde I am running PHP 5.6.30
This works as described if you set $tz to the correct timezone, but you have to do it manually. new \DateTimeZone(date_default_timezone_get()) sets the system default.
The 3rd parameter does absolutely bipkis.
@Kafoso Only if your default timezone happens to be GMT. If you're in any other timezone it's absolutely essential that you specify it.
25

Just use DateTime::setTimeZone():

$date = DateTime::createFromFormat('d/m/Y', $date);
$date->setTimeZone(new DateTimeZone('America/New_York'));
$date = $date->format('Y-m-d');

6 Comments

Can I somehow use for example +2:00 instead of America/New_York? I know you should use timezones and not offsets because of daylight savings etc. but on this environment timezones like America/New_York are not working.
See if this answer does what you need
Just to clarify. This does convert a date (user input and user's timezone) to the server timezone. So America/New_York is the timezone of the user?
Note that this example first creates $date in the server's timezone, and then when setTimeZone() is called, it converts it to "America/New_York". In other words, it introduces a time component - $date is no longer at midnight. Might not be a big deal for some, but it was for me!
Probably easiest way would be to create temp object and then new object base on temp. Like described below: $dateInServerTimeZone = DateTime::createFromFormat('d/m/Y', $dateString); $dateInWantedTimeZone = new DateTime($dateInServerTimeZone->format("Y-m-d H:i:s"), new DateTimeZone($createInZone)); Where $createInZone is a timzone name example "America/New_York" or "UTC" etc
|
0

I tested both the solutions JohnConde and denoise.

with this script:

$format = 'Y-m-d H:i:s';
$datetime_str = '2022-10-28 17:24:00';
$timezone_str = 'Europe/Rome';

$dt_test_denoise = DateTime::createFromFormat($format, $datetime_str, 
                                         new DateTimeZone($timezone_str));
echo '$dt_test_denoise:';
echo "<br/>";
var_dump($dt_test_denoise);
echo "<br/>";
echo "<br/>";

$dt_test_JohnConde = DateTime::createFromFormat($format, $datetime_str);
$dt_test_JohnConde->setTimeZone(new DateTimeZone($timezone_str));
echo '$dt_test_JohnConde:';
echo "<br/>";
var_dump($dt_test_JohnConde);
echo "<br/>";
echo "<br/>";

execute this on http://phptester.net/ and you will see the differences.

I think the best is solution denoise!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.