12

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.

yoshi
  • 1,209
  • 3
  • 15
  • 28

2 Answers2

29

(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'));
denoise
  • 967
  • 2
  • 14
  • 37
  • 8
    I don't know why this answer is not selected as solution. This is correct answer. – ofca Jun 14 '17 at 10:01
  • 2
    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 – Delphine Sep 26 '17 at 16:33
  • 1
    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. – j4k3 Nov 22 '17 at 11:00
  • The 3rd parameter does absolutely bipkis. – Kafoso Apr 03 '19 at 13:17
  • @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. – j4k3 Oct 30 '19 at 10:01
23

Just use DateTime::setTimeZone():

$date = DateTime::createFromFormat('d/m/Y', $date);
$date->setTimeZone(new DateTimeZone('America/New_York'));
$date = $date->format('Y-m-d');
John Conde
  • 212,985
  • 98
  • 444
  • 485
  • 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. – yoshi Jun 17 '14 at 20:13
  • 1
    See if [this answer](http://stackoverflow.com/a/7276486/250259) does what you need – John Conde Jun 17 '14 at 20:14
  • 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? – yoshi Jun 17 '14 at 20:35
  • 11
    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! – rinogo Jul 08 '16 at 22:53
  • 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 – Aivar Nov 06 '16 at 19:02