简体   繁体   English

PHP中的时区,日期,MySQL ......?

[英]Time Zones in PHP, Date, MySQL…?

I am trying to figure out how to display time in PST on my php site. 我试图弄清楚如何在我的PHP网站上显示PST的时间。 I have been putting in timestamps in the MySQL Database with NOW() and calling them like this: 我一直在使用NOW()在MySQL数据库中加入时间戳并像这样调用它们:

date("m/d/yg:ia", strtotime($ref['lastupdated'])) date(“m / d / yg:ia”,strtotime($ ref ['lastupdated']))

However, the server time is in Central and However, as the website is geared towards people in the PST time zone. 但是,服务器时间在中央,但是,因为该网站面向PST时区的人。 Basically it needs to show in PST. 基本上它需要在PST中显示。 Is there a way I could do this - like, take that value and subtract 2 or something? 有没有办法可以做到这一点 - 比如,拿下那个值并减去2或者什么? 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. 我不确定我是否需要解决PHP路由,或者问题是否可以通过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>';
    }
    }
$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 . 鉴于PST美国/洛杉矶 You have to make sure that date.timezone is set to the correct timezone for Central . 您必须确保将date.timezone设置为Central的正确时区。 This can either be accomplished via the php.ini or via date_default_timezone_set() . 这可以通过php.ini或通过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. 然后代码将您的传入字符串$ref['lastupdated']视为默认时区( Central ),并将日期时间转换为* America / Los_Angeles *时区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM