No announcement yet.

PHP Tips: Time and Tide wait for no dog...

  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP Tips: Time and Tide wait for no dog...

    A friend of mine had a problem. A causeway was covered at high tide and he wanted his website to indicate if the causeway was open or covered in water. He had the times when the causeway was covered, but how best to compare that with the present time?

    I find it's easiest to work with Unix timestamps. A Unix timestamp is the number of seconds since Jan 1st 1970. Being a simple number it makes comparisons easy, but how does that help when we only have times in hours and minutes?

    Fortunately PHPs function library comes to the rescue. strtotime() is a handy function which does its best to turn text strings (like '21:30', or '5pm', or 'next Tuesday') into a Unix timestamp. For more information see: PHP: strtotime - Manual A word of caution, beware of ambiguous strings such as 7/3/2009. Is that 7th March or July 3rd?

    To get the present time simply use the function time(). The rest is just simple comparison.

    Now where did I put my bucket and spade?
    black-dog Web stuff Band