1

I want to convert date time from the timezone UTC to any timezone like UTC+01 and also like timezone name e.g. Asia/Calcutta.

How can I do this?

Here is my code

<?php
date_default_timezone_set("UTC");
$date = new DateTime(date("Y-m-d H:i:s"));
$date->setTimezone(new DateTimeZone('UTC+01')); 
echo $date->format('Y-m-d H:i:s'); 
?>

I am getting the following error :

Fatal error: Uncaught exception 'Exception' with message 'DateTimeZone::__construct(): Unknown or bad timezone (UTC+01)' in /var/www/html/test/index.php:6 Stack trace: #0 /var/www/html/test/index.php(6): DateTimeZone->__construct('UTC+01') #1 {main} thrown in /var/www/html/test/index.php on line 6 
Faiyaz Alam
  • 1,191
  • 9
  • 27
  • http://stackoverflow.com/questions/3792066/convert-utc-dates-to-local-time-in-php – Lucky Chingi Dec 11 '15 at 14:17
  • 3
    RTFM: http://php.net/manual/en/timezones.php your timezone name is not one of the valid/recognized ones. And your code is pretty pointless. use the old `date()` to create a string for "now", which you then feed to datetime, which would have used "now" **ANYWAYS** to create `$date`. total waste of cpu cycles. – Marc B Dec 11 '15 at 14:19
  • @Marc B. I save the post in UTC timezone and now I want to show the date time as per client preference. He needs in UTC+01. I just posted the code from a test file not from my projec. – Faiyaz Alam Dec 11 '15 at 14:23
  • 1
    yes, and? look up the proper format to specify utc+1, which is available at the link I provided. you can't just make up your own timezone names and expect php to understand them. – Marc B Dec 11 '15 at 14:24
  • 1
    If you just want to add an hour why not just `$date->modify('+1 hour')`? https://eval.in/483818 – Darragh Enright Dec 11 '15 at 14:29
  • 1
    `new DateTime(date("Y-m-d H:i:s"))` is the same as `new DateTime`, no need for a `date` call. – deceze Dec 11 '15 at 14:39
  • 1
    "UTC+01" is not a *timezone*, it's an ***offset***. This offset will change throughout the year! If you want to localise a time to a client's location, **you need their location-based timezone** (Asia/Calcutta), not a UTC offset. A UTC offset only works if the client told you their offset just now; it doesn't work if you store it as a preference for future use. – deceze Dec 11 '15 at 14:42
  • @Marc B. Thanks for your support. I have on more question that how to convert this UTC+01 OR GMT+5:30 into timezone name. Is there any function for this? – Faiyaz Alam Dec 12 '15 at 14:48

1 Answers1

4

You have to use a valid time zone. Instead of using UTC+1 (which is invalid) you can use CET (central european time) or Europe/Berlin (both are depending on summer/wintertime), there are also countries where this timezone is valid for the whole year (UTC+1:00), for Calcutta, you can use Asia/Calcutta

Examples:

  • $date->setTimezone( new DateTimeZone( 'Asia/Calcutta' ) );
  • $date->setTimezone( new DateTimeZone( 'CET' ) );
  • $date->setTimezone( new DateTimeZone( 'Europe/Berlin' ) );

Here at the docs, you can find a list with valid DateTimeZone values.

Update: like mentioned in the comment from @Marc B. and @deceze you can just use

$date = new DateTime();

Instead of using

$date = new DateTime(date("Y-m-d H:i:s"));
swidmann
  • 2,787
  • 1
  • 18
  • 32
  • How do you say that UTC+1 is for Europe/Berlin and GMT+5:30 is for Asia/Calcutta. Is there any function which can convert timezone with offset like UTC+01 and GMT+5:30 into time zonename? – Faiyaz Alam Dec 12 '15 at 14:45
  • 1
    @ErFaiyazAlam: Well it was not correct to say that UTC+1 is Europe/Berlin, that is only the *half* truth, because of the summer and winter time. And I don´t know a build in function which would return you the timezone name just because of the given offset. I think it´s *pretty much impossible*. For example => UTC+8 would be correct for **8** different timezone name, so how to choose the correct one? There is a [function](http://php.net/manual/en/datetime.gettimezone.php) to get the timezone name, but that´s just the timezone from your script or the server. – swidmann Dec 12 '15 at 15:04
  • You can take a look at the [timezones and their offsets](http://www.timeanddate.com/time/zones/) – swidmann Dec 12 '15 at 15:05