Date/Time 函数
在线手册:中文 英文
PHP手册

date_parse_from_format

(PHP 5 >= 5.3.0)

date_parse_from_formatGet info about given date formatted according to the specified format

说明

array date_parse_from_format ( string $format , string $date )

Returns associative array with detailed info about given date.

参数

format

Format accepted by DateTime::createFromFormat().

date

String representing the date.

返回值

Returns associative array with detailed info about given date.

范例

Example #1 date_parse_from_format() example

<?php
$date 
"6.1.2009 13:00+01:00";
print_r(date_parse_from_format("j.n.Y H:iP"$date));
?>

以上例程会输出:

Array
(
    [year] => 2009
    [month] => 1
    [day] => 6
    [hour] => 13
    [minute] => 0
    [second] => 0
    [fraction] => 
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 0
    [errors] => Array
        (
        )

    [is_localtime] => 1
    [zone_type] => 1
    [zone] => -60
    [is_dst] => 
)

参见


Date/Time 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: Get info about given date formatted according to the specified format

用户评论:

crayonviolent at phpfreaks dot com (23-Mar-2011 05:02)

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
)

voidless at qip dot ru (10-Apr-2009 01:56)

Example of rewriting function for older PHP versions:

convert
April 8, 2009, 1:12 am
to
timestamp

<?php
function date2sql($str = ""){
$month_arr = array (
   
"January" => 1,
   
"February" => 2,
   
"March" => 3,
   
"April" => 4,
   
"May" => 5,
   
"June" => 6,
   
"July" => 7,
   
"August" => 8,
   
"September" => 9,
   
"October" => 10,
   
"November" => 11,
   
"December" => 12
);
preg_match('/([a-zA-Z]{3,9}) ([0-9]{1,2}), ([0-9]{4}),
([0-9]{1,2}):([0-9]{1,2}) ([ap])m/'
,$str,$matches2);
if (
$matches2[6]=="a") $hour = $matches2[4];
else
$hour = $matches2[4]+12;
if (
$hour==24) $hour=0;
return
mktime($hour,$matches2[5],0,
$month_arr[$matches2[1]],$matches2[2],$matches2[3]);
}
   
$time = date2sql("March 29, 2009, 12:12 pm");
    echo
"{$time}</br>\n";
?>