Documentation

Date and Time

Description

All dates are handled via API (inserting, retrieving, filters and etc.) in a current site format or, in case of admin section, in current language format.

The following functions are used for handling date and time:

Date and time format are specified using the designations listed below:

  • YYYY - year
  • MM - month
  • DD - dat
  • HH - hours
  • MI - minutes
  • SS - seconds

Starting from the main module version 11.5.4 supports the 12-hour time format and some new month display formats:

  • MMMM - full month (for display only);
  • M - first 3 month letters;
  • G - hour from 1-12 (without leading zero);
  • GG - 0-23 (without leading zero);
  • H - 01-12;
  • HH - 00-23;
  • TT - AM/PM (upper case);
  • T - am/pm.

Methods, used in SQL queries

  • CDatabase::CharToDateFunction - returns SQL code for converting string to the database time format

    <?
    $strSql = "
        SELECT 
            ID
        FROM 
            my_table
        WHERE 
            DATE_CREATE <= ".$DB->CharToDateFunction("10.01.2003 23:59:59")."
        ";
    $rs = $DB->Query($strSql, false, $err_mess.__LINE__);
    ?>

  • CDatabase::DateToCharFunction - returns SQL code for converting time from database time format into a sting.

    <?
    $strSql = "
        SELECT 
            ID, 
            ".$DB->DateToCharFunction("DATE_CREATE")." DATE_CREATE 
        FROM 
            my_table
        ";
    $rs = $DB->Query($strSql, false, $err_mess.__LINE__);
    ?>

  • CDatabase::CurrentDateFunction - returns SQL code for getting a current date.

    <?
    $strSql = "
        SELECT 
            ID 
        FROM 
            my_table 
        WHERE 
            DATE_CREATE<=".$DB->CurrentDateFunction()
        ";
    $rs = $DB->Query($strSql, false, $err_mess.__LINE__);
    ?>

  • CDatabase::CurrentTimeFunction - returns SQL code for getting current time in the database time format.

    <?
    $strSql = "
        UPDATE my_table SET 
             TIME_CHANGE=".$DB->CurrentTimeFunction()."
        WHERE 
             ID=45
        ";
    $Query($strSql, false, "FILE: ".__FILE__."<br>LINE: ".__LINE__);
    ?>

  • CDatabase::DatetimeToTimestampFunction - returns a valid timestamp without querying MakeTimeStamp.

    <?
    $strSql = "
        SELECT 
             ID, 
             ".$DB-> DatetimeToTimestampFunction("DATE_CREATE")." DATE_CREATE
        FROM 
             my_table
        ";
    $rs = $DB->Query($strSql, false, $err_mess.__LINE__);
    ?>

Format conversion

  • CDataBase::DateFormatToPHP - converts format, permitted in site settings into the format accepted in PHP.

    <?
    echo $DB->DateFormatToPHP("DD.MM.YYYY HH:MI:SS"); // d.m.Y H:i:s
    ?>

  • CDataBase::FormatDate - converts time from one string format to a string of arbitrary format.

    <?
    echo $DB->FormatDate("31.12.2005", "DD.MM.YYYY", "YYYY-MM-DD"); // 2005-12-31
    ?>

  • ConvertDateTime - converts time from one string format to a string of arbitrary format.

    MakeTimeStamp
    <?
    echo ConvertDateTime("25.12.2003", "YYYY-MM-DD", "ru"); // 2003-12-25
    ?>

  • MakeTimeStamp - converts time from a string into Unix format.

    <?
    echo MakeTimeStamp("07.04.2005 11:32:00", "DD.MM.YYYY HH:MI:SS"); // 1112862720
    ?>

  • ConvertTimeStamp - converts time from Unix format into a site format string.

    <?
    echo ConvertTimeStamp(mktime(0, 0, 0, 10, 25, 2003), "SHORT", "ru"); // 25.10.2003
    ?>

Additional functions

  • CDataBase::IsDate - checks for validity the date specified as string.

    <?
    echo $DB->IsDate("12.10.2005 22:34:15", "DD.MM.YYYY HH:MI:SS") ? "OK" : "ERROR"; // OK
    echo $DB->IsDate("12.13.2005 22:34:15", "DD.MM.YYYY HH:MI:SS") ? "OK" : "ERROR"; // ERROR
    echo $DB->IsDate("12.13..2005 ABS", "DD.MM.YYYY HH:MI:SS") ? "OK" : "ERROR"; // ERROR
    ?>

  • CDataBase::CompareDates - compares two dates specified as strings.

    <?
    echo $DB->CompareDates("01.01.2005", "01.01.2006"); // -1
    echo $DB->CompareDates("01.01.2006", "01.01.2005"); // 1
    echo $DB->CompareDates("01.01.2006", "01.01.2006"); // 0
    ?>

  • AddToTimeStamp - adds a specified time interval to the date in Unix format.

    <?
    $stmp = MakeTimeStamp("07.04.2005 11:32:00", "DD.MM.YYYY HH:MI:SS");
    $stmp = AddToTimeStamp(array("DD" => -1, "MM" => 1), $stmp); // 1115454720
    echo date("d.m.Y H:i:s", $stmp); // 06.05.2005 11:32:00
    ?>

  • ParseDateTime - returns an associative array describing the date specified as string.

    <?
    if ($arr = ParseDateTime("21.01.2004 23:44:15", "DD.MM.YYYY HH:MI:SS"))
    {
        echo "День:    ".$arr["DD"]."<br>";    // Day: 21
        echo "Месяц:   ".$arr["MM"]."<br>";    // Month: 1
        echo "Год:     ".$arr["YYYY"]."<br>";  // Year: 2004
        echo "Часы:    ".$arr["HH"]."<br>";    // Hours: 23
        echo "Минуты:  ".$arr["MI"]."<br>";    // Minutes: 44
        echo "Секунды: ".$arr["SS"]."<br>";    // Seconds: 15
    }
    else echo "Error!";
    ?>

  • getmicrotime - returns current time in Unix format.

    <?
    echo getmicrotime(); // 1115454720
    ?>

  • GetDateFormat - Returns site's date (time) format.

    <?
    // prints current date in current site format
    echo date($DB->DateFormatToPHP(CSite::GetDateFormat("SHORT")), time());
    ?>

  • GetTimeFormat - Returns time format, specified in site settings.

    <?
    echo $GLOBALS["DB"]->DateFormatToPHP( CSite::GetTimeFormat() );
    ?>


© «Bitrix24», 2001-2024