50 public static function fromString ($string, $useException = TRUE)
52 assert(is_string($string));
56 $year =
'(?P<year>\d{4})';
59 $month =
'(?P<month>0[1-9]|1[0-2])';
63 $day =
'(?P<day>0[1-9]|[0-2]\d|3[01])';
65 $hour =
'(?P<hour>[0-1]\d|2[0-3])';
67 $minute =
'(?P<minute>[0-5]\d)';
70 $second =
'(?P<second>[0-5]\d|60)';
72 $fraction =
'([.,](?P<fraction>\d+))';
76 $timezone =
'(?P<timezone>Z|[-+]([0-1]\d|2[0-3])([0-5]\d)?)';
83 "$year$month$day$hour".
89 if (preg_match($pattern, $string, $m)) {
90 if (empty($m[
'minute'])) {
93 if (empty($m[
'second'])) {
96 if (empty($m[
'fraction'])) {
99 $date =
new DateTime($m[
'year'].
'-'.$m[
'month'].
'-'.$m[
'day'].
'T'.$m[
'hour'].
':'.$m[
'minute'].
':'.$m[
'second'].
'.'.$m[
'fraction'].$m[
'timezone']);
100 $date->setTimezone(
new DateTimeZone(
'UTC'));
102 } elseif ($useException) {
114 public static function toString (DateTime $date, $setToUTC = TRUE)
117 $date->setTimezone(
new DateTimeZone(
'UTC'));
119 $fraction = preg_replace(
'/0+$/',
'', $date->format(
'u'));
120 $string = $date->format(
'YmdHis');
121 if (empty($fraction)) {
122 return preg_replace(
'/(00){1,2}$/',
'', $string).
'Z';
124 return $string.
'.'.$fraction.
'Z';
static toString(DateTime $date, $setToUTC=TRUE)
Convert from DateTime object to LDAP GeneralizedTime formatted string.
static fromString($string, $useException=TRUE)
Convert from LDAP GeneralizedTime formatted string to DateTime object.
LdapGeneralizedTime allows you to convert from and to LDAP GeneralizedTime format PHP DateTime object...