How to Determine if it is Friday the 13th with PHP

Josh Sherman
1 min read
Software Development PHP

It seemed fitting that my post on Friday the 13th would be about Friday the 13th. There are 52 Fridays in 2013, but only 2 of them are the 13th day of the month (September and December). To determine if a Friday is a Friday the 13th, we will need to use the date function along with the w format that will tell us the day of the week. w gives us the day of the week starting from zero but as of PHP 5.1.0 you could use N to get the day of the week starting at 1. I’ll be using w for backwards compatibility’s sake:

if (date('w', strtotime('2013-08-13')) == 5)
{
	echo 'You get to live another day!';
}

if (date('w', strtotime('2013-09-13')) == 5)
{
	echo 'Oh noes, you were killed by Jason Vorhees!';
}

You could simplify this by passing in time() but for the sake of example, I passed in explicit dates (and converted them to Unix timestamps).

Be safe this Friday the 13th and stay away from Camp Crystal Lake!

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

Related Articles