Documentation

ParseDateTime

mixed
ParseDateTime(
 string datetime,
 string format = FORMAT_DATETIME
);

The function parses the specified date and time using the passed formatting rules. Returns an array describing date and time according to the specified format, or false otherwise.

Function parameters

ParameterDescription
datetime Date and time in the format specified by format.
format Date and time format specifiers. Though the format is not strict, it is recommended to use system-wide specifiers:
  • YYYY - year
  • MM - month
  • DD - day
  • HH - hours
  • MI - minutes
  • SS - seconds
Optional parameter. By default, the [link=6658210#format_datetime] FORMAT_DATETIME[/link], constant value is used, which stores the current time format of the site (or language for the administrative section).

See Also

Examples of use

<?
$datetime = "21.01.2004 23:44:15";
$format = "DD.MM.YYYY HH:MI:SS";
echo "Source time: ".$datetime."<br>";
echo "Format: ".$format."<hr>";
if ($arr = ParseDateTime($datetime, $format))
{
    echo "Day:    ".$arr["DD"]."<br>";    // Day: 21
    echo "Month:   ".$arr["MM"]."<br>";    // Month: 1
    echo "Year:     ".$arr["YYYY"]."<br>";  // Year: 2004
    echo "Hours:    ".$arr["HH"]."<br>";    // Hours: 23
    echo "Minutes:  ".$arr["MI"]."<br>";    // Minutes: 44
    echo "Seconds: ".$arr["SS"]."<br>";    // Seconds: 15
}
else echo "Error!";
?>
<?
// display the date in the "21 January, 2004" format

$datetime = "21.01.2004"; // date is specified in the current site format

// FORMAT_DATETIME - constant containing the current site time format
$arr = ParseDateTime($datetime, FORMAT_DATETIME);

// 21 January, 2004
echo $arr["DD"]." ".ToLower(GetMessage("MONTH_".intval($arr["MM"])."_S")).", ".$arr["YYYY"];
?>
<?
// print the date of information block activity
// in arbitrary format

// include the information block module
if (CModule::IncludeModule("iblock"))
{
    // select an arbitrary element of information block
    $rsElement = CIBlockElement::GetByID(32675);
    $arElement = $rsElement->Fetch();

    // retrieve activity date
    // selected in the current site format
    $date_active = $arElement["ACTIVE_FROM"]; // 28.01.2005
    
    // get the current site time format
    $site_format = CSite::GetDateFormat(); // DD.MM.YYYY HH:MI:SS
get an array with the activity date of the element
    // get an array with description of activity date for the element. 
    if ($arr = ParseDateTime($date_active, $site_format))
    {
        /*
        Received $arr structure:
        Array
        (
            [DD] => 28
            [MM] => 1
            [YYYY] => 2005
            [HH] => 0
            [MI] => 0
            [SS] => 0
        )
        */

        // convert activity date into Unix format
        $stmp = mktime(
            $arr["HH"], $arr["MI"], $arr["SS"], 
            $arr["MM"], $arr["DD"], $arr["YYYY"]
            );

        // display the activity date of the selected element in arbitrary format
        // using the standard PHP date function
        echo date("d F Y", $stmp); // 28 January 2005
    }
}
?>


© «Bitrix24», 2001-2024
Up