note 103067 added to function.date-parse-from-format
| From: | crayonviolent at phpfreaks dot com | Date: | Wed, 23 Mar 2011 17:02:25 +0000 |
| Subject: | note 103067 added to function.date-parse-from-format | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178137@lists.php.net to get a copy of this message | ||
For those who do not have php5.3+, here is a php4+
compatible function that will approximate what this
function does.
NOTE: It does NOT exactly mimic it,
but it should be close enough for most
people's needs.
NOTE: The masks do NOT accurately
reflect the masks used in the date() format.
For simplicity sake they are just generic masks
for each date/time increment in order to parse
the $date string you pass.
<?php
function date_parse_from_format($format, $date) {
$dMask = array(
'H'=>'hour',
'i'=>'minute',
's'=>'second',
'y'=>'year',
'm'=>'month',
'd'=>'day'
);
$format = preg_split('//', $format, -1, PREG_SPLIT_NO_EMPTY);
$date = preg_split('//', $date, -1, PREG_SPLIT_NO_EMPTY);
foreach ($date as $k => $v) {
if ($dMask[$format[$k]]) $dt[$dMask[$format[$k]]] .= $v;
}
return $dt;
}
?>
Example 1:
<?php
print_r(date_parse_from_format('mmddyyyy','03232011');
?>
Output 1:
Array
(
[month] => 03
[day] => 23
[year] => 2011
)
Example 2:
<?php
print_r(date_parse_from_format('yyyy.mm.dd HH:ii:ss','2011.03.23 12:03:00'));
?>
Output 2:
Array
(
[year] => 2011
[month] => 03
[day] => 23
[hour] => 12
[minute] => 03
[second] => 00
)
----
Server IP: 64.71.164.2
Probable Submitter: 96.35.231.166
----
Manual Page -- http://www.php.net/manual/en/function.date-parse-from-format.php
Edit -- https://master.php.net/note/edit/103067
Del: integrated -- https://master.php.net/note/delete/103067/integrated
Del: useless -- https://master.php.net/note/delete/103067/useless
Del: bad code -- https://master.php.net/note/delete/103067/bad+code
Del: spam -- https://master.php.net/note/delete/103067/spam
Del: non-english -- https://master.php.net/note/delete/103067/non-english
Del: in docs -- https://master.php.net/note/delete/103067/in+docs
Del: other reasons-- https://master.php.net/note/delete/103067
Reject -- https://master.php.net/note/reject/103067
Search -- https://master.php.net/manage/user-notes.php