The last notes
All English-language materials have been translated fully automatically using the Google service
On D7, work with date / time is carried out using the Bitrix \ Main \ Type \ Date
and \Bitrix\Main\Type\DateTime
Class connection
use Bitrix \ Main \ Type \ DateTime;
Bitrix displays the date in the format "November 3"
strtolower(FormatDate("d F", MakeTimeStamp($arItem['ACTIVE_FROM'])))
Get date format settings from admin. parts
\ Bitrix \ Main \ Type \ DateTime :: getFormat () // will return, for example, "Y-m-d H: i: s"
Creating an object of class \Bitrix\Main\Type\DateTime
new \ Bitrix \ Main \ Type \ DateTime ("04/01/2020 12:00:00");
Create an object of class \ Bitrix \ Main \ Type \ DateTime
from the current time
new \ Bitrix \ Main \ Type \ DateTime (date ("d.m.Y H: i: s"));
Creating an object of the class \ Bitrix \ Main \ Type \ DateTime
from a custom format
$ dateTime = new \ Bitrix \ Main \ Type \ DateTime ("2020-04-01 12:00:00", "Y-m-d H: i: s");
Creating an object of class \ Bitrix \ Main \ Type \ DateTime
from an arbitrary format with an indication of the time zone
$ timeZone = new \ DateTimeZone ('Europe / Moscow'); // For Moscow
new \ DateTimeZone ('Asia / Novosibirsk'); // For Novosibirsk
$ dateTime = new \ Bitrix \ Main \ Type \ DateTime ("2020-04-01 12:00:00", "Y-m-d H: i: s", $ timeZone);
Creating an object of class \ Bitrix \ Main \ Type \ DateTime
from timestamp
$ dateTime = \ Bitrix \ Main \ Type \ DateTime :: createFromTimestamp (1585742400);
Creating an object of class \ Bitrix \ Main \ Type \ DateTime
from
$ phpDateTime = new DateTime ('2020-04-01');
$ dateTime = \ Bitrix \ Main \ Type \ DateTime :: createFromPhp ($ phpDateTime);
Creating an object of class \ Bitrix \ Main \ Type \ DateTime
from a text string
$ dateTime = \ Bitrix \ Main \ Type \ Date :: createFromText ("yesterday morning");
Get the time in the current site format
$dateTime->toString()
Get time in unix timestamp
$dateTime->getTimestamp()
Get time in any format
$ dateTime-> format ("Y-m-d H: i: s")
Get the current timezone
$dateTime->getTimeZone()
Checking for the object \ Bitrix \ Main \ Type \ DateTime
or \Bitrix\Main\Type\Date
if ($ dateTime instanceof \ Bitrix \ Main \ Type \ DateTime || $ dateTime instanceof \ Bitrix \ Main \ Type \ Date)
Adding and subtracting dates using human readable format:
$ dateTime-> add ("1 year + 3 months - 5 seconds");
Convert time format from bitrix format to php format:
\ Bitrix \ Main \ Type \ DateTime :: convertFormatToPhp ('YYYY-MM-DD HH-MI-SS') // "Y-m-d H-i-s"
Checking if the date is correct:
\ Bitrix \ Main \ Type \ DateTime :: isCorrect ("2018-06-14 02:20:00", "Y-m-d H: i: s")
Parse the date \ time object into components
$ datetime = "01/21/2004 23:44:15"; // Initial time
$ format = "DD.MM.YYYY HH: MI: SS"; //Format
if ($ arr = ParseDateTime ($ datetime, $ format))
{
echo "Day:". $ arr ["DD"]; // Day: 21
echo "Month:". $ arr ["MM"]; // Month: 1
echo "Year:". $ arr ["YYYY"]; // Year: 2004
echo "Hours:". $ arr ["HH"]; // Hours: 23
echo "Minutes:". $ arr ["MI"]; // Minutes: 44
echo "Seconds:". $ arr ["SS"]; // Seconds: 15
}
Difference between dates in days \ hours \ minutes
function dateDiff ($ date1, $ date2)
{
$ time = new DateTime ($ date1);
$ since_time = $ time-> diff (new DateTime ($ date2));
$ result ['days'] = $ since_time-> days;
$ result ['hours'] = $ since_time-> days * 24 + $ since_time-> h;
$ result ['minutes'] = ($ since_time-> days * 24 * 60) + ($ since_time-> h * 60) + $ since_time-> i;
return $ result;
}
Convert the timestamp to the usual format
function timestampToDate ($ timestamp, $ format = "d.m.Y H: i: s") {
return date ($ format, $ timestamp);
}
Subtract / add month / day to date, etc.
function dateDecrementByMonth ($ date_start) {// Subtract the month from the date
return date ("d-m-Y", strtotime ("- 1 month", strtotime ($ date_start)));
}
function dateIncrementByMonth ($ date_start) {// Add month to date
return date ("d-m-Y", strtotime ("+ 1 month", strtotime ($ date_start)));
}
Memo on accepted date format standards in different countries
Russia DD.MM.YYYY HH: MI: SS
USA MM-DD-YYYY HH: MI: SS
International English DD-MM-YYYY HH: MI: SS
UK DD / MM / YYYY HH: MI: SS
Based on material:
Comments