Mysql PHP 存储并显示正确用户时区的购买时间

发布于 2024-11-09 08:44:42 字数 315 浏览 0 评论 0原文

我有一个设置,我想将购买时间存储到精确秒。截至目前,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮色兮凉城 2024-11-16 08:44:42

一些事情:
1) 合并 Date & 是有意义的将时间列转换为单个 orderDate 列作为 MySQL 日期时间(然后您可以简单地使用 NOW() 作为插入值)

2)PHP 有很棒的文档:
http://php.net/manual/en/function.date.php

要为 UTC 时区站点访问者设置时区:

date_default_timezone_set('UTC'); // replace with variable, probably stored in user's session

顺便说一句,不确定 MySQL 是否提供即时设置时区(可能设置为服务器默认值),因此您可能必须使用上面的 php 日期函数来格式化 orderDate。

因此,总而言之,在使用 $q 的查询结果循环中,您的显示可能是:

echo date("Y-m-d h:i:s", strtotime($q[''orderDate]));

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:

date_default_timezone_set('UTC'); // replace with variable, probably stored in user's session

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:

echo date("Y-m-d h:i:s", strtotime($q[''orderDate]));
时光病人 2024-11-16 08:44:42

首先,我将存储 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

浅黛梨妆こ 2024-11-16 08:44:42

我将使用 UTC_TIMESTAMP 将您的时间值存储在 UTC 中,然后使用 ISO 日期格式获取它们:

SELECT DATE_FORMAT(UTC_TIMESTAMP, GET_FORMAT(DATETIME, 'ISO'));

稍前运行给出:

2011-05-22 17:53:23

所以假设用户在西海岸,我们可以做类似的事情这是他们的当地时间:

$timeZone = 'America/Los_Angeles';
$dateSrc = '2011-05-22 17:53:23';

$dateTime = new DateTime($dateSrc, new DateTimeZone('GMT'));
$dateTime->setTimeZone(new DateTimeZone($timeZone));
echo "Result is: " . $dateTime->format('Y-m-d H:i:s');

示例运行:

$ php test.php 
Result is: 2011-05-22 10:53:23

I would store your time values in UTC using UTC_TIMESTAMP, then grab them using an ISO date format:

SELECT DATE_FORMAT(UTC_TIMESTAMP, GET_FORMAT(DATETIME, 'ISO'));

Which run a bit ago gives:

2011-05-22 17:53:23

So let's say the user is in the west coast, we can do something like this to so their local time:

$timeZone = 'America/Los_Angeles';
$dateSrc = '2011-05-22 17:53:23';

$dateTime = new DateTime($dateSrc, new DateTimeZone('GMT'));
$dateTime->setTimeZone(new DateTimeZone($timeZone));
echo "Result is: " . $dateTime->format('Y-m-d H:i:s');

Sample run:

$ php test.php 
Result is: 2011-05-22 10:53:23
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文