Sara
Sara

Reputation: 59

Time Zones in PHP, Date, MySQL...?

I am trying to figure out how to display time in PST on my php site. I have been putting in timestamps in the MySQL Database with NOW() and calling them like this:

date("m/d/y g:i a", strtotime($ref['lastupdated']))

However, the server time is in Central and However, as the website is geared towards people in the PST time zone. Basically it needs to show in PST. Is there a way I could do this - like, take that value and subtract 2 or something? Any ideas?

I'm not sure also if I need to go about this the PHP route or if the problem, rather, could be solved via MySQL.

Any ideas are appreciated - thanks!

$query = "SELECT refid, CONCAT(fname,' ',lname) refname, email, street, city, state, zip, interestlvl, status, added, lastupdated FROM referrals WHERE affid='$affid' ORDER BY added DESC;";

$result = mysql_query($query) or die("Query Failed: ".mysql_errno()." - ".mysql_error()."<BR>\n$Query<BR>\n");


    if ($result) {
            while ($ref = mysql_fetch_array($result, MYSQL_ASSOC))      {
                echo '


    <td bgcolor="#EFEFEF" class="list">'.
        date_default_timezone_set('America/Chicago');
        $date = new DateTime($ref['lastupdated']);
        $date->setTimezone(new DateTimeZone('America/Los_Angeles'));
        $date->format('m/d/y g:i a')
        .'</td>';
    }
    }

Upvotes: 4

Views: 1438

Answers (1)

Stefan Gehrig
Stefan Gehrig

Reputation: 83622

$date = new DateTime($ref['lastupdated']);
$date->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $date->format('m/d/y g:i a');

given that PST is America/Los Angeles. You have to make sure that date.timezone is set to the correct timezone for Central. This can either be accomplished via the php.ini or via date_default_timezone_set().

The code then treats your incoming string $ref['lastupdated'] as being in the default timezone (Central) and converts the date-time into the America/Los_Angeles timezone.

Upvotes: 3

Related Questions