Bitrix Site Manager

ParseDateTime

mixed
ParseDateTime(
 string datetime,
 string format = FORMAT_DATETIME
)

The function ParseDateTime 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.

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. By default, the FORMAT_DATETIME constant value is used, which stores the time format of the site (or language for the administrative section).

See Also

Example



<?
$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>";
    echo "Month:   ".$arr["MM"]."<br>";
    echo "Year:    ".$arr["YYYY"]."<br>";
    echo "Hours:   ".$arr["HH"]."<br>";
    echo "Minutes: ".$arr["MI"]."<br>";
    echo "Seconds: ".$arr["SS"]."<br>";
}
else echo "Error!";
?>

<?
// display the date in the format "21 January, 2004"

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

// the constant FORMAT_DATETIME 
// contains the time format of the current site
$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 module
if (CModule::IncludeModule("iblock"))
{
    // select an element
    $rsElement = CIBlockElement::GetByID(32675);
    $arElement = $rsElement->Fetch();

    // obtain the activity date
    // in the current site format
    $date_active = $arElement["ACTIVE_FROM"]; // 28.01.2005
    
    // get the current site format
    $site_format = CSite::GetDateFormat(); // DD.MM.YYYY HH:MI:SS

    // get an array with the activity date of the element
    if ($arr = ParseDateTime($date_active, $site_format))
    {
        /*
        $arr structure:
        Array
        (
            [DD] => 28
            [MM] => 1
            [YYYY] => 2005
            [HH] => 0
            [MI] => 0
            [SS] => 0
        )
        */

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

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