Bitrix Site Manager

CDatabase::FormatDate

string
CDatabase::FormatDate(
 string date,
 string from_format = "DD.MM.YYYY HH:MI:SS",
 string to_format = "DD.MM.YYYY HH:MI:SS"
)

The method FormatDate converts the date from the source format into another. The format allows the following symbols.
Formatter Description
YYYY Year (0001 - 9999)
MM Month (01 - 12)
DD Day (01 - 31)
HH Hours (00 - 24)
MI Minutes (00 - 59)
SS Seconds (00 - 59)

Parameters

ParameterDescription
date Date to be converted.
from_format Format of the date to be converted. Optional; the default value is "DD.MM.YYYY HH:MI:SS".
to_format Target format to convert into. Optional; the default value is "DD.MM.YYYY HH:MI:SS".

See Also

Example



<?
// specify the date
$date = "31.12.2005";

// set the input date format
$format = "DD.MM.YYYY";

// obtain the format of the current site
$new_format = CSite::GetDateFormat("SHORT"); // YYYY-MM-DD

// perform conversion
$new_date = $DB->FormatDate($date, $format, $new_format);

// got the date in the new format!
echo $new_date; // 2005-12-31
?>
<?
// convert the date from the format of one site to that of another

// obtain the format of the "de" site
$format_de = CSite::GetDateFormat("SHORT", "de"); // DD.MM.YYYY

// obtain the format of the "en" site
$format_en = CSite::GetDateFormat("SHORT", "en"); // YYYY-MM-DD

// perform conversion
$new_date = $DB->FormatDate($date, $format_de, $format_en);

// got the date in the new format!
echo $date; // 2005-12-31
?>