LSldap: add formatDate() method

This commit is contained in:
Benjamin Renard 2023-03-20 16:42:32 +01:00
parent 4ec5769276
commit 1825ce429e
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -332,6 +332,27 @@ class LSldap extends LSlog_staticLoggerClass {
);
}
/**
* Format a Datetime object as LDAP date string
*
* @param DateTime|null $datetime The DateTime object to format (optional, default: now)
* @param bool $utc Force UTC timezone (optional, default: false)
*
* @return string|false LDAP date string, or false
*/
public static function formatDate($datetime=null, $utc=true) {
if (is_null($datetime))
$datetime = new DateTime('now');
elseif (!$datetime instanceof DateTime)
return false;
$datetime -> setTimezone(timezone_open($utc?'utc':date_default_timezone_get()));
$datetime_string = $datetime -> format('YmdHis.uO');
// Replace +0000 or -0000 end by Z
$datetime_string = preg_replace('/[\+\-]0000$/', 'Z', $datetime_string);
return $datetime_string;
}
/**
* Check if an attribute exists in specified attributes collection
*