Working with dates in php in Bitrix and not only
Rus
Eng
Работа с датой на php в Битрикс и не только

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

classes

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

There are no comments yet, you can be the first to leave it

Leave a comment

The site uses a comment pre-moderation system, so your message will be published only after approval by the moderator

You are replying to a user's comment

Send

FEEDBACK

Email me

Are you developing a new service, making improvements to the existing one and want to be better than your competitors? You have come to the right place. I offer you a comprehensive studio-level website development. From me you can order design, layout, programming, development of non-traditional functionality, implementation of communication between CMS, CRM and Data Analitics, as well as everything else related to sites, except for promotion.

Contact, I will always advise on all questions and help you find the most effective solution for your business. I am engaged in the creation of sites in Novosibirsk and in other regions of Russia, I also work with the CIS countries. You will be satisfied with our cooperation

An error occurred while sending, please try again after a while
Message sent successfully

Phones

+7(993) 007-18-96

Email

info@tichiy.ru

Address

Россия, г. Москва

By submitting the form, you automatically confirm that you have read and accept the Privacy Policy site

Contact with me
Send message
By submitting the form, you automatically confirm that you have read and accept Privacy policy of site
Sending successful!
Thank you for contacting :) I will contact you as soon as possible
Sending failed
An error occurred while sending the request. Please wait and try again after a while or call my phone number