Mysql PHP 存储并显示正确用户时区的购买时间
我有一个设置,我想将购买时间存储到精确秒。截至目前,我使用 CURTIME() 来实现此目的。现在它以 16:03:59 的格式返回时间,如果我想存储然后显示正确时区的时间(例如下午 2:03),我该怎么办?谢谢。
$qry = "INSERT INTO purchases
(id, qty, Date, Time, product_id, totalprice)
VALUES('$id', '$qty', CURDATE(), CURTIME(),'$pid', '$price')";
$result = @mysql_query($qry);
I have a setup where I want to store the time of a purchase down to the exact second. As of right now I use CURTIME() for this. Right now it returns the time in this format 16:03:59 what should I do if I want to store and then display the time in the correct time zone like 2:03 p.m.? Thanks.
$qry = "INSERT INTO purchases
(id, qty, Date, Time, product_id, totalprice)
VALUES('$id', '$qty', CURDATE(), CURTIME(),'$pid', '$price')";
$result = @mysql_query($qry);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一些事情:
1) 合并 Date & 是有意义的将时间列转换为单个 orderDate 列作为 MySQL 日期时间(然后您可以简单地使用 NOW() 作为插入值)
2)PHP 有很棒的文档:
http://php.net/manual/en/function.date.php
要为 UTC 时区站点访问者设置时区:
顺便说一句,不确定 MySQL 是否提供即时设置时区(可能设置为服务器默认值),因此您可能必须使用上面的 php 日期函数来格式化 orderDate。
因此,总而言之,在使用 $q 的查询结果循环中,您的显示可能是:
Couple things:
1) would make sense to consolidate Date & Time columns into a single, say, orderDate column as MySQL date-time (then you can use simply, NOW() for the insert value)
2) PHP has great docs:
http://php.net/manual/en/function.date.php
To set the timezone for UTC timezone site visitor:
btw, not sure if MySQL provides setting timezone on-the-fly (probably set to server default), so you'll likely have to format the orderDate using php's date function above.
So, to sum up, in your query result loop using $q, your display could be:
首先,我将存储 time() 的值,然后根据需要转换为适当时区的字符串。例如,如果用户在旅行时更改时区,这会变得更加优雅。
要格式化用户的时区,您可以使用 Javascript 获取时区,然后将其传递给 PHP 并进行相应的格式化。之前已经问过/回答过这个问题:
Javascript/PHP 和时区
Firstly, I would store the value of time(), then do conversions to a string in the appropriate timezone as necessary. This makes it a little bit more elegant if the user changes his timezone while travelling for example.
To format for the users' timezone, you could get the timezone using Javascript, then pass it to PHP and format accordingly. This has been asked/answered before:
Javascript/PHP and timezones
我将使用
UTC_TIMESTAMP
将您的时间值存储在 UTC 中,然后使用 ISO 日期格式获取它们:稍前运行给出:
所以假设用户在西海岸,我们可以做类似的事情这是他们的当地时间:
示例运行:
I would store your time values in UTC using
UTC_TIMESTAMP
, then grab them using an ISO date format:Which run a bit ago gives:
So let's say the user is in the west coast, we can do something like this to so their local time:
Sample run: