处理时间跨度的最佳方式?

发布于 2024-08-03 17:24:29 字数 65 浏览 4 评论 0原文

PHP 中是否有处理时间跨度的首选类或方法?我感兴趣的主要功能是检查日期是否在时间跨度内,或生成下限和上限的时间戳。

Is there a preferred class or method for working with timespans in PHP? The primary functionality I am interested in is checking if a date is within the timespan, or generating timestamps for the lower and upper limits.

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

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

发布评论

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

评论(5

红衣飘飘貌似仙 2024-08-10 17:24:29

使用unix时间戳。如果是mysql数据,那么你可以像这样存储时间戳,如果不是,那么你也可以将mysql日期时间转换为unix时间戳。

php.net 站点上有大量有关此功能的文档,但它们都相对简单且易于使用。

祝你好运。

use the unix timestamp. If it's mysql data, then you can store timestamps like this, if not then you can also convert mysql datetimes to unix timestamps.

There is a bunch of documentation on the functionality pertaining to this at the php.net site, but it's all relatively simple and easy to use.

Good luck.

慵挽 2024-08-10 17:24:29

还有 DateTime 类,它对于处理时间很有用。

There's also the DateTime class, which is useful for dealing with time.

放我走吧 2024-08-10 17:24:29

我更喜欢使用unix时间戳,然后你可以检查

if($time > $lower && $time < $upper)
  echo $time is inside bounds

i prefer using unix timestamps, you can then check

if($time > $lower && $time < $upper)
  echo $time is inside bounds
沧笙踏歌 2024-08-10 17:24:29

根据我的经验(主要是时间数据采集系统),缺乏适当的“Span”支持库很快就会导致跳出窗口的错误。

我的建议:创建一个 Span 类,从一开始就预见它要处理包含和独占端点。能够创建具有不同包含语义的跨度通常非常重要:January = Span (1/1, 31/1) = Span (1/1, 1/2(, ....

In my experience (time data acquisition systems, mainly), the absence of a proper "Span" supporting library leads to jump-out-of-the-window bugs quite soon.

My advice: create a Span class, foresee it to handle inclusive and exclusive endpoints from the beginning. It's often quite important to be able to create spans with different inclusion semantics: January = Span (1/1, 31/1) = Span (1/1, 1/2(, ....

莫多说 2024-08-10 17:24:29

如果您的跨度从未跨越其边界(1970/01/01 到 2037/03/xx),那么绝对是 Unix 时间戳。如果您不需要知道跨度的长度(2009/03/05 和 2009/08/22 之间有多少天?),那么 YYYYMMDDHHMMSS 格式(或其他类似格式)的字符串可以轻松地确定日期时间是否为其他两个日期时间值之间。

Definitely the Unix timestamp if your spans never cross its boundaries (1970/01/01 to 2037/03/xx). If you never need to know the length of a span (how many days between 2009/03/05 and 2009/08/22?) then strings in YYYYMMDDHHMMSS format (or other similar formats) work just as easily for determining if a datetime is between two other datetime values.

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