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

I am trying to figure out how to display the time in PST on my php site. I put timestamps in the MySQL database using the NOW () command and called them like this:

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

However, server time is at the center, and yet, since the website is targeted at people in the PST time zone. Basically it needs to be shown in PST. Is there a way I could do this - for example, take this value and subtract 2 or something else? Any ideas?

I'm also not sure that I need to go this way of PHP, or if the problem can be solved through MySQL rather.

Any ideas 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/yg:i a') .'</td>'; } } 
+4
source share
1 answer
 $date = new DateTime($ref['lastupdated']); $date->setTimezone(new DateTimeZone('America/Los_Angeles')); echo $date->format('m/d/yg:i a'); 

given that PST is America / Los Angeles. You must make sure that the date.timezone parameter date.timezone set to the correct time zone for Central. This can be achieved using php.ini or via date_default_timezone_set() .

The code then treats your input string $ref['lastupdated'] as being in the default time zone (Central) and converting the date-time to the time zone * America / Los_Angeles *.

+3
source

Source: https://habr.com/ru/post/1338467/


All Articles