Dyce & Sons Ltd.

Helping IT since 1993

PHP Solves Marital Disputes!

Tuesday 20th August, 2013

Did you know that you can use PHP to solve marital disputes? Well, yep, you can. Assuming they involve english to date translation.

As you may know the strtotime() function in PHP is a wonderful thing. Stick in some English time definitions, and you get out a timestamp, which you can use, for well other stuff.

It turns out that an argument over whether this Sunday and last Sunday are the same thing can easily be solved using a simple test:

echo "strtotime('last sunday') = ".strtotime('last sunday')."\n";
echo "strtotime('next sunday') = ".strtotime('next sunday')."\n";
echo "strtotime('this sunday') = ".strtotime('this sunday')."\n";

For the adventurous - try running that on a Sunday, Monday, and Friday. Do you get the same answer?

Of course, as a programmer, one should always be a little more specific…

echo "strtotime('+1 sunday') = ".strtotime('+1 sunday')."\n";
echo "strtotime('-1 sunday') = ".strtotime('-1 sunday')."\n";

… is what you really meant, right?