Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you change the timezone in PHP for an existing timestamp?

The code for the date and time function:

function date_and_time($format,$timestamp) {

$date_and_time = date($format,$timestamp);
return $date_and_time;

}

And then the code to display it:

    <?php

        echo date_and_time("dS F Y", strtotime($profile[last_activity_date_and_time]));

    ?>

The value of $profile[last_activity_date_and_time] is 2010-01-18 14:34:04

When displayed it shows up as 18th January 2010 - 02:34pm

But, is there any way to change the timezone it is displayed in?

like image 945
Ryan Avatar asked Jan 19 '10 16:01

Ryan


People also ask

How do I convert one time zone to another in PHP?

We can also use the function date_default_timezone_set to set the default timezone and use a different timezone to convert the date and time from the default timezone to the given timezone as shown below. This is the easiest way to convert the time from one clock to another using the DateTime and DateTimeZone classes.

How can I modify the PHP timezone setting for my website?

Open Hosting → Manage → PHP Configuration page. There, open the PHP options tab and edit the date. timezone value: If you are not sure which time zone to insert, check the Time Zone Map.

What timezone is UTC for PHP?

The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts. See PHP's list of supported timezones to find the names of all possible timezones you can use for the date.

Where is date timezone in PHP INI?

ini file is located at the /etc directory. Depending on the installed version of PHP version, you will find a [Date] section like the following. If it is not there, just add a line with the "date. timezone = Your/Timezone" function.


1 Answers

Not sure if this what you're looking for, but try DateTime

date_default_timezone_set('Europe/London');

$datetime = new DateTime();
$datetime->setTimestamp($yourTimestamp);
echo $datetime->getTimezone()->getName();
echo $datetime->format(DATE_ATOM);

$la_time = new DateTimeZone('America/Los_Angeles');
$datetime->setTimezone($la_time);
echo $datetime->getTimezone()->getName();
echo $datetime->format(DATE_ATOM);
like image 102
Gordon Avatar answered Oct 31 '22 12:10

Gordon