PHP’s strtotime – Interesting Fallback for Relative Formats

Written by Grzegorz Rajchman on February 9, 2016

Today I made an interesting discovery: strtotime function in PHP will accept any string from 0 to 6 characters as a number of hours but with opposite sign.

strtotime('+7') === strtotime('-7 hours'); // true
strtotime('+6 wows') === strtotime('-6 hours'); // true
strtotime('+5 miles') === strtotime('-5 hours'); // true
strtotime('+4 meters') === strtotime('-4 hours'); // true
strtotime('-3 yards') === strtotime('+3 hours'); // true
strtotime('+2 hours') === strtotime('-2 hours'); // false, obviously
strtotime('+1 something') === strtotime('-1 hours'); // false, string is too long


I’ve tried to make sense of this by looking at the source code and the documentation but I couldn’t find anything that would explain that behaviour.

So a word of advice is: take extra care if you want to use this function, especially with user input.