-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fbff356
commit 71a75af
Showing
4 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<html> | ||
<head> | ||
|
||
</head> | ||
<body> | ||
<p> | ||
{{ timebetween(date1, date2) }} | ||
</p> | ||
</body> | ||
</html> |
72 changes: 72 additions & 0 deletions
72
src/JMBTechnologyLimited/Twig/Extensions/TimeBetweenExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace JMBTechnologyLimited\Twig\Extensions; | ||
|
||
/** | ||
* Takes two DateTime objects. | ||
* Returns string of time between them. | ||
* | ||
* With a lot of inspiration from https://github.com/twigphp/Twig-extensions/blob/fe6f308b5aa35b64ae1687f1f6f510e4e59c033e/lib/Twig/Extensions/Extension/Date.php | ||
* (License: https://github.com/twigphp/Twig-extensions/blob/e740ab2e3c97b54028f21f9f8314caf6fdc482d7/LICENSE ) | ||
* | ||
* @license https://github.com/JMB-Technology-Limited/Twig-Extensions/blob/master/LICENSE.txt 3-clause BSD | ||
* @copyright (c) JMB Technology Limited, http://jmbtechnology.co.uk/ | ||
*/ | ||
class TimeBetweenExtension extends \Twig_Extension | ||
{ | ||
|
||
const FUNCTION_NAME_TIMEBETWEEN = 'timebetween'; | ||
|
||
public static $units = array( | ||
'y' => 'year', | ||
'm' => 'month', | ||
'd' => 'day', | ||
'h' => 'hour', | ||
'i' => 'minute', | ||
's' => 'second', | ||
); | ||
|
||
const LABEL_ONE = 'one'; | ||
const LABEL_PLURAL = 's'; | ||
|
||
|
||
public function getFilters() | ||
{ | ||
return array(); | ||
} | ||
|
||
public function getFunctions() | ||
{ | ||
return array( | ||
new \Twig_SimpleFunction(static::FUNCTION_NAME_TIMEBETWEEN, array($this, 'timebetween')), | ||
); | ||
} | ||
|
||
public function timebetween($date1,$date2) { | ||
if(get_class($date1) != 'DateTime') return ''; | ||
if(get_class($date2) != 'DateTime') return ''; | ||
|
||
$diff = $date1->diff($date2); | ||
|
||
foreach (self::$units as $attribute => $unit) { | ||
$count = $diff->$attribute; | ||
if (0 !== $count) { | ||
return $this->getPluralizedInterval($count, $diff->invert, $unit); | ||
} | ||
} | ||
return ''; | ||
} | ||
|
||
protected function getPluralizedInterval($count, $invert, $unit) | ||
{ | ||
if (1 == $count) { | ||
return self::LABEL_ONE . " " . $unit; | ||
} else { | ||
return $count . " " . $unit. self::LABEL_PLURAL; | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
/* | ||
* @license https://github.com/JMB-Technology-Limited/Twig-Extensions/blob/master/LICENSE.txt 3-clause BSD | ||
* @copyright (c) JMB Technology Limited, http://jmbtechnology.co.uk/ | ||
*/ | ||
class TimeBetweenTest extends TestCase { | ||
|
||
function timeBetweenTestProvider() { | ||
$utc = new \DateTimeZone('UTC'); | ||
return array( | ||
array(new DateTime('2015-01-01 10:00:00', $utc), new DateTime('2015-01-01 12:00:00', $utc), '2 hours'), | ||
array(new DateTime('2015-01-01 10:00:00', $utc), new DateTime('2015-01-02 12:00:00', $utc), 'one day'), | ||
array(new DateTime('2015-01-01 10:00:00', $utc), new DateTime('2015-01-01 10:30:00', $utc), '30 minutes'), | ||
array(new DateTime('2015-01-01 10:00:00', $utc), new DateTime('2015-02-01 10:30:00', $utc), 'one month'), | ||
array(new DateTime('2015-01-01 10:00:00', $utc), new DateTime('2016-02-01 10:30:00', $utc), 'one year'), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider timeBetweenTestProvider | ||
*/ | ||
function testTimeBetween($in1, $in2, $out) { | ||
$ext = new \JMBTechnologyLimited\Twig\Extensions\TimeBetweenExtension(); | ||
$this->assertEquals($out, $ext->timebetween($in1, $in2)); | ||
} | ||
|
||
/** | ||
* | ||
* It shouldn't matter in which order you pass the dates. | ||
* @dataProvider timeBetweenTestProvider | ||
*/ | ||
function testTimeBetweenReversed($in1, $in2, $out) { | ||
$ext = new \JMBTechnologyLimited\Twig\Extensions\TimeBetweenExtension(); | ||
$this->assertEquals($out, $ext->timebetween($in2, $in1)); | ||
} | ||
|
||
} |