Skip to content

Instantly share code, notes, and snippets.

@ryanmerritt
Created September 18, 2012 10:17
Show Gist options
  • Save ryanmerritt/3742433 to your computer and use it in GitHub Desktop.
Save ryanmerritt/3742433 to your computer and use it in GitHub Desktop.
Verify a date string is ISO 8601 formatted in PHP
// Regex from http://www.pelagodesign.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
function assertISO8601Date($dateStr) {
if (preg_match('/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/', $dateStr) > 0) {
return TRUE;
} else {
return FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment