Milksnake12
Milksnake12

Reputation: 561

Changing timezones with Date function in PHP

Im trying to assign a variable with timezone info that will change the time of info saved in a database.

Here is my code that doesn't work:

<?php 
$timeZone = "- 3600";
$date = date('His', time() $timeZone);
echo $date;
?>

But when I do this it works...

<?php 
$date = date('His', time() - 3600);
echo $date;
?>

Why won't the variable work in there?

Upvotes: 0

Views: 937

Answers (3)

Ben Dubuisson
Ben Dubuisson

Reputation: 757

If you want to convert to another timezone, there is a proper way of doing it: UTC Date/Time String to Timezone

Upvotes: 1

Kermit
Kermit

Reputation: 34063

It's invalid syntax. Change $timeZone to an integer and add it to time().

$timeZone = -3600;
$date = date('His', time() + $timeZone);
echo $date;

See a demo

Upvotes: 1

Sam Dufel
Sam Dufel

Reputation: 17598

You can't embed binary operators in a string and expect them to execute. You'll need to do something like this:

<?php 
$timeZone = -3600;
$date = date('His', time() + $timeZone);
echo $date;
?>

Upvotes: 0

Related Questions