PHP date_parse_from_format()函数

PHP date_parse_from_format()函数

定义和用法

date_parse_from_format()函数接受一个格式字符串和一个日期字符串作为参数,并以指定的格式返回有关给定日期的信息。

语法

date_parse($date)

参数

序号 参数与描述
1 format(必需) 这是一个字符串值,表示您需要格式化日期信息的格式。
2 date(必需) 这是一个字符串值,表示您需要获取有关的日期的信息。

返回值

PHP date_create_from_format() 函数返回一个数组,包含指定格式的给定日期的信息。

PHP版本

该函数在PHP版本5.3.0中首次引入,并且适用于后续所有版本。

示例

以下示例演示了 date_parse_from_format() 函数的用法。

<?php
   //Creating a DateTime object
   date = "25-Mar-1989";format = "d-M-Y";
   res = date_parse_from_format(format, date);
   print_r(res);
?>

这将产生以下结果:

Array
(
    [year] => 1989
    [month] => 3
    [day] => 25
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] =>
)

示例

让我们看看解析日期的不同格式:

<?php
   res1 = date_parse_from_format("j.n.Y", "25.8.2014");
   print_r(res1);

   res2 = date_parse_from_format("y-d-m", "2014-25-8");
   print_r(res2);

   res3 = date_parse_from_format("n/j/y", "8/25/2014");
   print_r(res3);

   res4 = date_parse_from_format("D.M.Y", "25.8.2014");
   print_r(res4);

   res5 = date_parse_from_format("H/i/s", "12/32/25");
   print_r(res5);
?>

这将产生以下结果−

Array
(
    [year] => 2014
    [month] => 8
    [day] => 25
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] =>
)
Array
(
    [year] => 2020
    [month] => 25
    [day] => 14
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 1
    [warnings] => Array
        (
            [7] => The parsed date was invalid
        )

    [error_count] => 2
    [errors] => Array
        (
            [2] => The separation symbol could not be found
            [7] => Trailing data
        )

    [is_localtime] =>
)
Array
(
    [year] => 2020
    [month] => 8
    [day] => 25
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 1
    [errors] => Array
        (
            [7] => Trailing data
        )

    [is_localtime] =>
)
Array
(
    [year] => 8
    [month] =>
    [day] =>
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 4
    [errors] => Array
        (
            [0] => A textual day could not be found
            [3] => The separation symbol could not be found
            [4] => Trailing data
        )

    [is_localtime] =>
)
Array
(
    [year] =>
    [month] =>
    [day] =>
    [hour] => 12
    [minute] => 32
    [second] => 25
    [fraction] => 0
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] =>
)

示例

以下示例演示了使用相对格式的date_parse_from_format()函数−

<?php
   print_r(date_parse_from_format("Y-m-d", "2009-18-18-+52 week +25 hour"));
   print("\n");
   print_r(date_parse_from_format("Y-m-d", "1990-06-06 +52 week +25 hour"));
?>

这将会产生以下输出−

Array
(
    [year] => 2009
    [month] => 18
    [day] => 18
    [hour] =>
    [minute] =>
    [second] =>
    [fraction] =>
    [warning_count] => 1
    [warnings] => Array
        (
            [10] => The parsed date was invalid
        )

    [error_count] => 1
    [errors] => Array
        (
            [10] => Trailing data
        )

    [is_localtime] =>
)

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

    [error_count] => 1
    [errors] => Array
        (
            [10] => Trailing data
        )

    [is_localtime] =>
)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程