1

I have a timezone of the user(he chooses it from a list) I have a time in UTC(not current time) So I need something like GetTimeForRegion(time, timezone) for PHP. Is there such functions or libraries or services?

Cœur
  • 37,241
  • 25
  • 195
  • 267
whn
  • 321
  • 3
  • 11
  • 1
    This post seems to be similar. http://stackoverflow.com/questions/952975/how-can-i-easily-convert-dates-from-utc-via-php – gokujou Feb 16 '11 at 15:25

4 Answers4

8

you can use DateTime::setTimezone(). If your UTC date is an UNIX timestamp, you can use some code like this :

$date = new DateTime();
$date->setTimezone(new DateTimeZone('UTC'));
$date->setTimestamp(1297869844);
$date->setTimezone(new DateTimeZone('Europe/Paris'));

echo $date->format('Y-m-d H:i:s');
// Will print 2011-02-16 16:24:04
Artefact2
  • 7,516
  • 3
  • 30
  • 38
1

date('r') or date('c') may help you.

echo date('r') prints Thu, 16 Feb 2011 16:01:07 +0200

echo date('c') prints 2011-02-16T16:01:07+02:00

aiternal
  • 1,080
  • 3
  • 16
  • 33
0

You need to look at the Date/Time API in PHP. I strongly advise you to stay away of gmdate and older date functions in php.

In your case, you should ask the user for its Olson based time zone.

The code of Artefact2 will do the trick.

Jerome WAGNER
  • 21,986
  • 8
  • 62
  • 77
0

please write this instead :
$date = date("Y-m-d H:i:s" , time());

so Y it means year
m means month
d means day H get hours from 0 - 24
h get hours from 0 to 12 i get minutes
s get seconds