diff --git a/CHANGELOG.md b/CHANGELOG.md index 072c02c..b115f4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ All notable changes to this project will be documented in this file. +## 1.3.0 - 2018-06-22 + +### Added +- Format the time as a `DateInterval` +- Output the total seconds, minutes and hours + +### Changed +- Both the filter and function now return a `TimeModel` + ## 1.2.1 - 2018-06-19 ### Fixed diff --git a/README.md b/README.md index 50f1982..f2d1285 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,48 @@ Returns: 9 minutes, 40 seconds Returns: 10 minutes ``` +## TimeModel + +Whenever you're dealing with the read time in your template, you're actually working with a TimeModel object. + +### Simple Output + +Outputting a TimeModel object without attaching a property or method will return the time in the form of a human time duration. + +``` +{{ string|readTime }} + +{{ readTime(entry) }} +``` + +### Properties + +#### `human` + +The human time duration. + +#### `interval(format)` + +A `DateInterval` object. You'll need to set the [format](http://php.net/manual/en/dateinterval.format.php) as a parameter: + +```twig +{% set time = readTime(entry) %} + +{{ time.interval('%h hours, %i minutes, $s seconds') }} +``` + +#### `seconds` + +The total number of seconds. + +#### `minutes` + +The total number of minutes. + +#### `hours` + +The total number of hours. + ## Overriding Plugin Settings If you create a [config file](https://docs.craftcms.com/v3/configuration.html) in your `config` folder called `read-time.php`, you can override the plugin’s settings in the Control Panel. Since that config file is fully [multi-environment](https://docs.craftcms.com/v3/configuration.html) aware, this is a handy way to have different settings across multiple environments. diff --git a/composer.json b/composer.json index c48c527..ea78e63 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "lukeyouell/craft-readtime", "description": "Calculate the estimated read time for content.", "type": "craft-plugin", - "version": "1.2.1", + "version": "1.3.0", "keywords": [ "craft", "cms", diff --git a/src/models/TimeModel.php b/src/models/TimeModel.php new file mode 100644 index 0000000..11cb0ae --- /dev/null +++ b/src/models/TimeModel.php @@ -0,0 +1,66 @@ +human(); + } + + public function human() + { + return DateTimeHelper::secondsToHumanTimeDuration($this->seconds, $this->showSeconds); + } + + public function interval($format = '%h hours, %i minutes, %s seconds') + { + $currentTimeStamp = DateTimeHelper::currentTimeStamp(); + $datetimeStart = DateTimeHelper::toDateTime($currentTimeStamp); + $datetimeEnd = DateTimeHelper::toDateTime(DateTimeHelper::currentTimeStamp() + $this->seconds); + + $interval = $datetimeStart->diff($datetimeEnd); + + return $interval->format($format); + } + + public function seconds() + { + return $this->seconds; + } + + public function minutes() + { + return floor($this->seconds / 60); + } + + public function hours() + { + return floor(($this->seconds / 60) / 60); + } +} diff --git a/src/twigextensions/ReadTimeTwigExtension.php b/src/twigextensions/ReadTimeTwigExtension.php index c0b2965..0a9c22f 100644 --- a/src/twigextensions/ReadTimeTwigExtension.php +++ b/src/twigextensions/ReadTimeTwigExtension.php @@ -11,6 +11,7 @@ namespace lukeyouell\readtime\twigextensions; use lukeyouell\readtime\ReadTime; +use lukeyouell\readtime\models\TimeModel; use Craft; use craft\helpers\DateTimeHelper; @@ -70,17 +71,24 @@ public function readTimeFunction($element, $showSeconds = true) } } - $duration = DateTimeHelper::secondsToHumanTimeDuration($totalSeconds, $showSeconds); + $data = [ + 'seconds' => $totalSeconds, + 'showSeconds' => $showSeconds, + ]; - return $duration; + return new TimeModel($data); } public function readTimeFilter($value = null, $showSeconds = true) { $seconds = $this->valToSeconds($value); - $duration = DateTimeHelper::secondsToHumanTimeDuration($seconds, $showSeconds); - return $duration; + $data = [ + 'seconds' => $seconds, + 'showSeconds' => $showSeconds, + ]; + + return new TimeModel($data); } // Private Methods