CodingHero
CodingHero

Reputation: 683

PHP - get date by timezone

How I can get the current date of a specific timezone, then convert it to my timezone? For example:

$timezone = "UTC+1"; // or UTC, UTC-5, UTC+2....
$date = date($timezone); 
$date_UTC = convert($date, "UTC");

So the final return must be something like "2014-02-14 08:25:00 pm +00";

I played around with many functions, but most of them they use timezone like "Europe/France".

Thanks

Upvotes: 0

Views: 3384

Answers (3)

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13263

You can use the datetime extension to set timezones:

$timezone = new DateTimeZone('Europe/Copenhagen');
$datetime = new DateTime('now', $timezone);
echo '<pre>In Copenhagen: ', $datetime->format('r'), '</pre>';

$timezone = new DateTimeZone('Europe/Helsinki');
$datetime->setTimezone($timezone);
echo '<pre>In Helsinki: ', $datetime->format('r'), '</pre>';

Which would give you something like:

In Copenhagen: Fri, 14 Feb 2014 22:01:31 +0100

In Helsinki: Fri, 14 Feb 2014 23:01:31 +0200

Here is a list of supported timezones.

Upvotes: 0

Amal Murali
Amal Murali

Reputation: 76666

You can achieve this by using DateTime class in conjunction with DateTimeZone class:

$current_tz_str = date_default_timezone_get();
$current_tz = new DateTimeZone($current_tz_str);
$now = new DateTime('now', $current_tz);
$offset = $current_tz->getOffset($now);

Upvotes: 1

knx
knx

Reputation: 398

First change the timezone with:

date_default_timezone_set('Europe/London');

date_default_timezone_set — Sets the default timezone used by all date/time functions in a script

see php.net/

Upvotes: 0

Related Questions