note 81611 added to function.strptime
| From: | firefox3107 at gmail dot com | Date: | Thu, 06 Mar 2008 07:01:08 +0000 |
| Subject: | note 81611 added to function.strptime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136781@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Server IP: 212.124.37.9
Probable Submitter: 83.181.36.105
----
Manual Page -- http://www.php.net/manual/en/function.strptime.php
Edit -- https://master.php.net/note/edit/81611
Del: integrated -- https://master.php.net/note/delete/81611/integrated
Del: useless -- https://master.php.net/note/delete/81611/useless
Del: bad code -- https://master.php.net/note/delete/81611/bad+code
Del: spam -- https://master.php.net/note/delete/81611/spam
Del: non-english -- https://master.php.net/note/delete/81611/non-english
Del: in docs -- https://master.php.net/note/delete/81611/in+docs
Del: other reasons-- https://master.php.net/note/delete/81611
Reject -- https://master.php.net/note/reject/81611
Search -- https://master.php.net/manage/user-notes.php