How to Determine if it is Friday the 13th with 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!

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

If you found this article helpful, please consider buying me a coffee.