I've spent many a sleepless night working with dates in programming. Few things as basic as dates strike fear into a developer's heart. On the surface they seem just like any other component of an application that is easily overcome given the slightest bit of thought and some basic math. However, this 5-minute problem quickly turns into a migraine once you consider timezones, bi-annual clock-changes, leap-years, and extra seconds being added to a year.
Side Note: If you ever find yourself having trouble falling asleep, give The ical Spec a good looking over. It's a tribute to how absolutely convoluted our calendar system has become.
If you ever catch yourself working with dates, there is one function you need to be aware of.
strtotime()
int strtotime ( string $time [, int $now ] )
strtotime — Parse about any English textual datetime description into a Unix timestamp
What does this mean? You can pass any time format to this function, and it will return a Unix timestamp. This thing could take a kitchen sink and it would return a timestamp. Not only will it take any common date/time format, it will accept math-like parameters.
Not only will all of these work:
strtotime("10 September 2000"), "n"; strtotime("8/10/2000"), "n"; strtotime("Sept 10, 2000"), "n";
But these too:
strtotime("+1 day"), "n"; strtotime("+1 week"), "n"; strtotime("+1 week 2 days 4 hours 2 seconds"), "n"; strtotime("next Thursday"), "n"; strtotime("last Monday"), "n";
What if you had a timestamp and wanted to get the previous day? strtotime() has you covered:
$my_timestamp = strtotime("Jan 1, 2000"); strtotime("-1 day", $my_timestamp);
strtotime() takes a second parameter has an integer timestamp so you are able to use the date addition/subtraction functions at any point in time. This is a crude example but it has countless applications.
Hopefully, this will make someone's day. strtotime() is rediculously robust and useful.


