Documentation

DateTime

DateTime class for handling dates and time. Majority of date within the core are object of this class. The class itself is an extension to the class Date.

Queries the following namespace:

Object of class \Datetime is practically an analog to PHP class \DateTime, but is not inherited from it. Object of class \DateTime can be retrieved from PHP class object via the method \Bitrix\Main\Type\DateTime::createFromPhp or from timestamp using \Bitrix\Main\Type\DateTime::createFromTimestamp:

$objDateTime = DateTime::createFromPhp(new \DateTime('2000-01-01'));

$objDateTime = DateTime::createFromTimestamp(1346506620);

Constructors:

// Current time:
$objDateTime = new DateTime();
// From the string in current site format
$objDateTime = new DateTime("25.12.2012 12:30:00");
// From string with specified format:
$objDateTime = new DateTime("2007-05-14 12:10:00", "Y-m-d H:i:s");

Method Description Available from version
createFromTimestamp Creates DateTime object from Unix timestamp.
createFromPhp Creates a \DateTime PHP DateTime object.
createFromUserTime Creates a DateTime object from local user time with global timezone settings and default regional settings factored in.
getTimeZone Returns timezone object.
setDefaultTimeZone Sets timezone by default.
setTimeZone Sets timezone object.
toString Converts date into string using regional settings and global timezone settings.
toUserTime Updates time from server time to user time using global timezone settings.

Examples

How to get representation from object as timestamp:

echo $objDateTime->getTimestamp();

How to get the view as string in current site format from object:

echo $objDateTime->toString();

How to get the view in arbitrary format from object (using \Bitrix\Main\Type\DateTime::format):

echo $objDateTime->format("Y-m-d H:i:s");

Implement summation and substraction by indicating dates using words as follows: years, months, days, weeks, hours, minutes, seconds and signs +/-:

$objDateTime = new DateTime("01.01.2012 00:00:00"); // "2012-01-01 00:00:00"
$objDateTime->add("1 day"); // "2012-01-02 00:00:00"
$objDateTime->add("-1 day"); // "2011-12-31 00:00:00"
$objDateTime->add("3 months - 5 days + 10 minutes"); // "2012-03-27 00:10:00"

Offset can be specified in the following format: DateInterval (however, the letter "P" at the start of string is optional):

$objDateTime = new DateTime("01.01.2012 00:00:00"); // "2012-01-01 00:00:00"
$objDateTime->add("7M5DT2M"); // "2012-08-06 00:02:00"
$objDateTime->add("-2YT10M"); // "2009-12-31 23:50:00"

When indicating the offset, specify periods from higher item to lower (for example, year, month, hours).

Time from current site format:

$date = new \Bitrix\Main\Type\DateTime("16.08.2014 15:30:10");

$arFields = Array(
   'DATE_START' => \Bitrix\Main\Type\DateTime::createFromUserTime("16.08.2014 15:30:10"); 
);

Filter by datetime:

//for current date 
$filter = array(
   " new \Bitrix\Main\Type\DateTime(),
   ">DATE_END" => new \Bitrix\Main\Type\DateTime(), 
);


© «Bitrix24», 2001-2022
Up