note 59748 added to function.strtotime
| From: | ScottB at osu1 dot php dot net | Date: | Wed, 14 Dec 2005 21:36:43 +0000 |
| Subject: | note 59748 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100160@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59748
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59748&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59748&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59748&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59748&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59748&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59748&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59748&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59748&report=yes
Search -- http://master.php.net/manage/user-notes.php