4

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>';
    }
    }

1 Answer 1

3
$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.

Sign up to request clarification or add additional context in comments.

5 Comments

This works great, but sometimes I get the value returned like this: 102/04/11 11:56 am . There is an odd "1" appearing from no where. I've checked that it's not in the HTML. Any clue? Thank you!
2011-02-04 13:56:44 is the value from MySQL. Then I get 102/04/11 11:56 am from the code. :O Even if I change it to like, M/d/y I get 1Feb/04/11
Please show all of the relevant code... I highly doubt that the snippet above produces this output.
Sorry, I posted the relevant code in the original thread, as the comment was formatting it weird!
I got it! I was putting an echo in an echo. I'm sorry for that! Thank you SO much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.