tags:

views:

302

answers:

5

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.

+4  A: 

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.

Evernoob
+3  A: 

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

nikc
Any comment on why I got the downvote?
nikc
+1  A: 

i prefer using unix timestamps, you can then check

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

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.

jmucchiello
+1  A: 

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

xtofl