note 59748 deleted from function.strtotime by derick

From: Date: Sun, 15 Jul 2007 12:44:37 +0000
Subject: note 59748 deleted from function.strtotime by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-128565@lists.php.net to get a copy of this message
Note Submitter: ScottB ---- strtotime assumes a US-style date in the format mm/dd/yy. This function assumes a UK-style dd/mm/yy format, converts it to US-style and passes it to strtotime. The date parts can be separated with slashes, hypens, dots or spaces. Unambiguous dates such as "2005-05-04" or "4 May 05" pass through unaltered (they don't match the regex). // Returns a timestamp from a string. Assumes en_GB format where ambiguous. function parsedate($value) { // If it looks like a UK date dd/mm/yy, reformat to US date mm/dd/yy so strtotime can parse it. $reformatted = preg_replace("/^\s*([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $value); return strtotime($reformatted); } e.g. parsedate("13/11/05"); // 13th November, UK-style parsedate("4/5/05"); // 4th May not 5th April parsedate("13 nov 05"); // Unambiguous parsedate("2005-11-13"); // Unambiguous

« previous php.notes (#128565) next »