note 50422 deleted from function.strtotime by derick
| From: | derick@php.net | Date: | Sun, 14 Aug 2005 11:57:52 +0000 |
| Subject: | note 50422 deleted from function.strtotime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-93603@lists.php.net to get a copy of this message | ||
Note Submitter: Guilherme Blanco
----
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")));
?>