在 php 中接受国际日期时间输入

发布于 2024-12-27 10:12:55 字数 381 浏览 1 评论 0原文

我目前正在向用 PHP 编写的系统添加对国际化的支持。所有日期现在都存储为 UTC,并根据个人用户本地化偏好显示。

但是,当用户输入日期时间(例如指定某个时间窗口)时,他们输入的日期时间会被解释为 UTC 日期时间,而不是本地日期时间。为了完成国际化,系统需要假设用户输入的日期时间是指他们的当地时间。

如何将日期字符串(即“YYYY-MM-DD HH:MM”)转换为 unix 时间戳以实现正确的本地化?

  • 进一步澄清 - 数据库中的所有日期 = UTC 时间戳 所有 HTML 页面都显示用户当地时间(按照其设置中的定义) HTML 表单的日期默认为当前本地时间 PHP 必须将该日期时间视为本地时间而不是 UTC PHP 必须将此本地日期时间字符串转换为 UTC 时间戳

I'm currently adding support for internationalisation to a system written in PHP. All dates are now stored as UTC, and displayed according to individual user localisation preferences.

However when a user inputs a date time (such as to specify a certain time window), the date time they input gets interpreted as a UTC datetime, not their local datetime. For the internationalisation to be complete the system needs to assume that a datetime entered by the user refers to their local time.

How do I convert a date string (ie 'YYYY-MM-DD HH:MM') into a unix timestamp for the correct localisation?

  • Further Clarification -
    All dates in database = UTC Timestamps
    All HTML Pages display users local time (as defined in their settings)
    HTML form has date that defaults to current local time
    PHP must treat that date time as local not UTC
    PHP must convert this local date timestring into UTC timestamp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夕色琉璃 2025-01-03 10:12:55

假设您知道用户的时区,他可能在某处的首选项中选择了该时区:

$timezone = new DateTimeZone($usersTimezone);
$datetime = new DateTime('2012-01-18 20:00:00', $timezone);
echo $datetime->getTimestamp();

这需要最新版本的 PHP 与 日期时间

Assuming you know the timezone of the user, which he presumably chose in the preferences somewhere:

$timezone = new DateTimeZone($usersTimezone);
$datetime = new DateTime('2012-01-18 20:00:00', $timezone);
echo $datetime->getTimestamp();

This requires a recent version of PHP with DateTime.

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