note 81611 deleted from function.strptime by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 03:49:32 +0000 |
| Subject: | note 81611 deleted from function.strptime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142092@lists.php.net to get a copy of this message | ||
Note Submitter: firefox3107 at gmail dot com
----
It's not the same as strptime. The array keys are different, for example '%d' will
produce 'd' => '12'!!!
But it also returns false on failure.
<?php
function strToTime($date, $format) {
$search = array('%d', '%D', '%j', // day
'%m', '%M', '%n', // month
'%Y', '%y', // year
'%G', '%g', '%H', '%h', // hour
'%i', '%s');
$replace = array('(\d{2})', '(\w{3})', '(\d{1,2})', //day
'(\d{2})', '(\w{3})', '(\d{1,2})', // month
'(\d{4})', '(\d{2})', // year
'(\d{1,2})', '(\d{1,2})', '\d{2}', '\d{2}', // hour
'(\d{2})', '(\d{2})');
$pattern = str_replace($search, $replace, $format);
if(!preg_match("#$pattern#", $date, $matches)) {
return false;
}
$dp = $matches;
if(!preg_match_all('#%(\w)#', $format, $matches)) {
return false;
}
$id = $matches['1'];
if(count($dp) != count($id)+1) {
return false;
}
$ret = array();
for($i=0, $j=count($id); $i<$j; $i++) {
$ret[$id[$i]] = $dp[$i+1];
}
return $ret;
}
?>