问题
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