The Complex Made Simple

For work, I had a piece of code that needed to cal­cu­late if a cer­tain date fell in between two other days. So I started out writ­ing out this big long func­tion to cal­cu­late 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, inac­cu­rate, com­plex and just plain stu­pid of me to do it that way.

I knew there had to be an eas­ier way so off to PHP.net I went and sure enough there is str­to­time. Str­to­time con­verts a string into a Unix time­stamp. Turn­ing my func­tion 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;
}

Remem­ber when in doubt: KISS (Keep it sim­ple stupid).

Comments are disabled for this post