Skip to content

Commit

Permalink
Merge pull request #709 from CrAzE124/master
Browse files Browse the repository at this point in the history
Added Util::convertDateTimeObject to support converting \DateTime object...
  • Loading branch information
ruflin committed Oct 22, 2014
2 parents 0d2400b + ec910f5 commit e1163b1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CHANGES

2014-10-22
- Added Util::convertDateTimeObject to Util class to easily convert \DateTime objects to required format #708

2014-10-10
- Fixed Response::isOk() to work better with bulk update api

Expand Down
17 changes: 17 additions & 0 deletions lib/Elastica/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ public static function convertDate($date)
return $string;
}

/**
* Convert a \DateTime object to format: 1995-12-31T23:59:59Z+02:00
*
* Converts it to the lucene format, including the appropriate TimeZone
*
* @param \DateTime $dateTime
* @param boolean $includeTimezone
* @return string
*/
public static function convertDateTimeObject(\DateTime $dateTime, $includeTimezone = true)
{
$formatString = 'Y-m-d\TH:i:s' . ($includeTimezone === true ? 'P' : '\Z');
$string = $dateTime->format($formatString);

return $string;
}

/**
* Tries to guess the name of the param, based on its class
* Example: \Elastica\Filter\HasChildFilter => has_child
Expand Down
24 changes: 24 additions & 0 deletions test/lib/Elastica/Test/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,28 @@ public function testConvertRequestToCurlCommand()
$this->assertEquals($expected, $curlCommand);

}

public function testConvertDateTimeObjectWithTimezone()
{
$dateTimeObject = new \DateTime();
$timestamp = $dateTimeObject->getTimestamp();

$convertedString = Util::convertDateTimeObject($dateTimeObject);

$date = date('Y-m-d\TH:i:sP', $timestamp);

$this->assertEquals($convertedString, $date);
}

public function testConvertDateTimeObjectWithoutTimezone()
{
$dateTimeObject = new \DateTime();
$timestamp = $dateTimeObject->getTimestamp();

$convertedString = Util::convertDateTimeObject($dateTimeObject, false);

$date = date('Y-m-d\TH:i:s\Z', $timestamp);

$this->assertEquals($convertedString, $date);
}
}

0 comments on commit e1163b1

Please sign in to comment.