File tree 3 files changed +44
-0
lines changed
3 files changed +44
-0
lines changed Original file line number Diff line number Diff line change 1
1
CHANGES
2
2
3
+ 2014-10-22
4
+ - Added Util::convertDateTimeObject to Util class to easily convert \DateTime objects to required format #708
5
+
3
6
2014-10-10
4
7
- Fixed Response::isOk() to work better with bulk update api
5
8
Original file line number Diff line number Diff line change @@ -119,6 +119,23 @@ public static function convertDate($date)
119
119
return $ string ;
120
120
}
121
121
122
+ /**
123
+ * Convert a \DateTime object to format: 1995-12-31T23:59:59Z+02:00
124
+ *
125
+ * Converts it to the lucene format, including the appropriate TimeZone
126
+ *
127
+ * @param \DateTime $dateTime
128
+ * @param boolean $includeTimezone
129
+ * @return string
130
+ */
131
+ public static function convertDateTimeObject (\DateTime $ dateTime , $ includeTimezone = true )
132
+ {
133
+ $ formatString = 'Y-m-d\TH:i:s ' . ($ includeTimezone === true ? 'P ' : '\Z ' );
134
+ $ string = $ dateTime ->format ($ formatString );
135
+
136
+ return $ string ;
137
+ }
138
+
122
139
/**
123
140
* Tries to guess the name of the param, based on its class
124
141
* Example: \Elastica\Filter\HasChildFilter => has_child
Original file line number Diff line number Diff line change @@ -74,4 +74,28 @@ public function testConvertRequestToCurlCommand()
74
74
$ this ->assertEquals ($ expected , $ curlCommand );
75
75
76
76
}
77
+
78
+ public function testConvertDateTimeObjectWithTimezone ()
79
+ {
80
+ $ dateTimeObject = new \DateTime ();
81
+ $ timestamp = $ dateTimeObject ->getTimestamp ();
82
+
83
+ $ convertedString = Util::convertDateTimeObject ($ dateTimeObject );
84
+
85
+ $ date = date ('Y-m-d\TH:i:sP ' , $ timestamp );
86
+
87
+ $ this ->assertEquals ($ convertedString , $ date );
88
+ }
89
+
90
+ public function testConvertDateTimeObjectWithoutTimezone ()
91
+ {
92
+ $ dateTimeObject = new \DateTime ();
93
+ $ timestamp = $ dateTimeObject ->getTimestamp ();
94
+
95
+ $ convertedString = Util::convertDateTimeObject ($ dateTimeObject , false );
96
+
97
+ $ date = date ('Y-m-d\TH:i:s\Z ' , $ timestamp );
98
+
99
+ $ this ->assertEquals ($ convertedString , $ date );
100
+ }
77
101
}
You can’t perform that action at this time.
0 commit comments