note 50422 added to function.strtotime
| From: | GuilhermeBlanco at osu1 dot php dot net | Date: | Mon, 28 Feb 2005 03:27:17 +0000 |
| Subject: | note 50422 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85652@lists.php.net to get a copy of this message | ||
I made a simple hack to allow a correct date parse of Quarters.
For example, of you have this date: Q1 2005, it should be converted to 2005-03-27. The only support
it doesn't have is for 2 digit year based (because of my Regular Expression).
So, these entries are allowed:
- Q7 2005 => will be converted to Q3 2006
- Q1 2005 10:01:50
- February 2005
- 16-feb-2006
- and other strtotime compatible.
The function:
<?php
function str2time($str)
{
$str = preg_replace("/Q(.+){1}\s+([[:digit:]]{4})(.*)/i", "\\2-X\\1X-27\\3",
trim($str));
preg_match_all("/X(.+)X/i", $str, $matches);
$quarter = $matches[1][0];
$month = $quarter * 3;
if (strlen($month) == 1) $month = "0".$month;
return strtotime(str_replace("X{$quarter}X", $month, $str));
}
?>
Example call:
<?php
print_r(getdate(str2time("Q1 2005 10:01:50")));
?>
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50422
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50422&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50422&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50422&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50422&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50422&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50422&report=yes
Search -- http://master.php.net/manage/user-notes.php