27 Jul

bluesmoon – Handling Date/Times and timezones

  • Always store time in UTC
  • Convert to local time on display (local being defined by the user looking at the data)
  • When storing a timezone, you need the name, timestamp and the offset. This is required because governments sometimes change the meanings of their timezones (eg: the US govt changed DST dates), and your application needs to handle things gracefully… eg: The exact timestamp when episodes of LOST showed both before and after DST rules changed.

via bluesmoon – Handling Date/Times and timezones. The three basic rules of storing date/times in a web application.

27 Jan

The Complex Made Simple

For work, I had a piece of code that needed to calculate if a certain date fell in between two other days. So I started out writing out this big long function to calculate if a date fell first within the same year as either of the two dates, and if so within the two months, and within the two days. It was ugly, inaccurate, complex and just plain stupid of me to do it that way.

I knew there had to be an easier way so off to PHP.net I went and sure enough there is strtotime. Strtotime converts a string into a Unix timestamp. Turning my function into this:

function in_between_two_dates($first_date, $second_date, $third_date)
{
if(strtotime($first_date) >= strtotime($second_date) && strtotime($first_date) <= strtotime($third_date) )
{ return true; }
else return false;
}

Remember when in doubt: KISS (Keep it simple stupid).