Rishi
Rishi

Reputation: 21

PHP Making Time variables? 5 for the hour and then echo it back?

Just a quick question, how would I be able to edit the following Date/Time variable to PLUS(+) the hour number 5, at the moment it's in US time, I need to convert everything to UK time.

$tme = date('H:i : d F Y');

Any ideas...?

Upvotes: 1

Views: 125

Answers (5)

Madara's Ghost
Madara's Ghost

Reputation: 175098

Use DateTime objects, it's more robust and will give you less headaches:

$time = new DateTime("now", new DateTimeZone("America/New_York"));
$time->setTimezone(new DateTimeZone("Europe/London"));

echo $time->format("H:i:s Y-m-d");

Also note that with this, you can deal with weird timezone related oddities (such as some timezones changing in different dates, some are dependent of years etc) without doing any sort of computation, the DateTimeZone object does it for you :-)

Upvotes: 6

FtDRbwLXw6
FtDRbwLXw6

Reputation: 28929

Use DateTime and DateTimeZone instead:

// U.S. timezone (EST)
$tme = new DateTime('now', new DateTimeZone('America/New_York'));
echo $tme->format('H:i : d F Y');

// U.K. timezone
$tme->setTimeZone(new DateTimeZone('Europe/London'));
echo $tme->format('H:i : d F Y');

Upvotes: 0

Peter
Peter

Reputation: 16943

$tme = date('H:i : d F Y', time() + 60*5);

but you may be interested in DateTime object

or change timezone using

date_default_timezone_set('Europe/London');

Upvotes: 0

Dr. Dan
Dr. Dan

Reputation: 2288

Use date_default_timezone_set function

date_default_timezone_set('Europe/London');

Upvotes: 1

Josh
Josh

Reputation: 2895

Add this to the top of your page:

date_default_timezone_set('Europe/London');

Upvotes: 1

Related Questions