在 php 中接受国际日期时间输入
我目前正在向用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您知道用户的时区,他可能在某处的首选项中选择了该时区:
这需要最新版本的 PHP 与
日期时间
。Assuming you know the timezone of the user, which he presumably chose in the preferences somewhere:
This requires a recent version of PHP with
DateTime
.