0

I have the UTC time 15:00:00

timezone based UTC offset is +03:00

when I tried to create the new time using utc time+utc offset , it shows 12:00:00.

but i need the time is 18:00:00

please give me the suggestions.

$UTC_offset="+03:00";   
date("d M, Y h:i A", strtotime("15:00:00 ".$UTC_offset))
axiac
  • 68,258
  • 9
  • 99
  • 134
Karthikeyan Ganesan
  • 1,901
  • 20
  • 23

2 Answers2

2

The code you posted parses the string '15:00:00 +03:00.

In plain English, this string means "the local time is 15:00 (3 PM) and I am located in a timezone that is 3 hours at the East of GMT/UTC". The corresponding UTC time is 12:00.

It happens that your PHP has the default timezone set to UTC (I guess this is the default value in php.ini and you didn't change it). This is why date() prints 07 Sep, 2016 12:00 PM.

For me your code prints 07 Sep, 2016 03:00 PM because I am also in a timezone at UTC+3 and my php.ini uses it as default.

Don't use date() (or any of the date/time functions). Some of them don't know anything about the timezone, others work only with local time.

Use the DateTime classes instead. They can handle the timezones and the code is shorter and more clear.

Your concrete problem is easy to solve:

// Parse the input time in the UTC timezone
$date = new DateTime("15:00:00", new DateTimeZone("UTC"));
// Change the timezone to local
$date->setTimeZone(new DateTimeZone("+03:00"));
// Output the local time
echo($date->format("d M, Y h:i A"));

It displays:

07 Sep, 2016 06:00 PM

Read more about the DateTime, DateTimeZone, DateTimeInterval and the other DateTime classes. They are the modern way of handling date & time in PHP.

axiac
  • 68,258
  • 9
  • 99
  • 134
0

As commenter Marc B. indicates, subtract the offset instead of adding it. For a suggestion, have a look at the php manual PHP: Date/Time Functions and possibly consider setting a default time zone (rather than manually modifying) with date_default_time_zone_get and ...set. There are many avenues to potentially pursue.

If you want the customers local time, an easy way is to use client side javacript. See SO question How can I get the user's local time instead of the server's time?

Community
  • 1
  • 1
RigidBody
  • 656
  • 3
  • 11
  • 26
  • but i have the customers from many countries. so the default timezone set to UTC and customer based UTC offset taken from the database and finally display the customer action timing in customer's invoice – Karthikeyan Ganesan Sep 07 '16 at 16:08