Best way to work with timespans?

天大地大妈咪最大 提交于 2020-01-14 14:00:09

问题


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.


回答1:


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.




回答2:


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




回答3:


i prefer using unix timestamps, you can then check

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



回答4:


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(, ....




回答5:


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.



来源:https://stackoverflow.com/questions/1342247/best-way-to-work-with-timespans

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!