From f7e709ac8af6384ce31f1978ca17201855dceb1c Mon Sep 17 00:00:00 2001 From: Nuno Chaves Date: Mon, 22 Jun 2015 15:58:14 +0100 Subject: [PATCH 1/9] Initial commit --- LICENSE | 22 ++++++++++++++++++++++ README.md | 2 ++ 2 files changed, 24 insertions(+) create mode 100644 LICENSE create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..55986dd --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Nuno Chaves + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..243684d --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Scheduler +PHP Simple Scheduler with one time or recurring events From 62ca9b0746ad37efce46f1a6811a3022ab0f31aa Mon Sep 17 00:00:00 2001 From: Nuno Date: Wed, 15 Jul 2015 17:43:35 +0100 Subject: [PATCH 2/9] initial commit --- .gitignore | 15 +++++++ README.md | 68 +++++++++++++++++++++++++++- src/Scheduler/Scheduler.php | 88 +++++++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 src/Scheduler/Scheduler.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cdbf1f1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +# temp files from editors +*~ +*.bak +.DS_Store +Thumbs.db +.buildpath +.project +.settings +*.tmproj +build +.idea +composer.phar +nbproject/ +vendor/ +examples/db.php \ No newline at end of file diff --git a/README.md b/README.md index 243684d..71a03e1 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,66 @@ -# Scheduler -PHP Simple Scheduler with one time or recurring events +# jupitern/scheduler +#### PHP Scheduler. + +add one time event dates or recurring event dates +get next event date or next X event dates from a given date + +## Requirements + +PHP 5.4 or higher. + +## Installation + +Include jupitern/datatables in your project, by adding it to your composer.json file. +```javascript +{ + "require": { + "jupitern/scheduler": "0.*" + } +} +``` + +## Usage +```php +// instance Scheduler +$schedules = \Lib\Dispatcher\Scheduler::instance() + +// add a one time event date +// accepts any string compatible with php DateTime object +->add('2020-01-01 12:35') + +// add another one time event date +// accepts any string compatible with php DateTime object +->add('2020-01-01 17:50') + +// add a recurring date +// accepts any string compatible with php DateTime object +->addRecurring('+ 8 hours') + +// get next 5 schedules starting at 2020-01-01 +->getNextSchedules('2020-01-01 00:00:00', 5); + +// display schedules +foreach ($schedules as $schedule) { + echo $schedule->format('Y-m-d H:i').PHP_EOL; +} + +/* +output: +2020-01-01 08:00 +2020-01-01 12:35 +2020-01-01 16:00 +2020-01-01 17:50 +2020-01-01 00:00 +*/ + +``` + +## Contributing + + - welcome to discuss a bugs, features and ideas. + +## License + +jupitern/table is release under the MIT license. + +You are free to use, modify and distribute this software, as long as the copyright header is left intact diff --git a/src/Scheduler/Scheduler.php b/src/Scheduler/Scheduler.php new file mode 100644 index 0000000..556e6ca --- /dev/null +++ b/src/Scheduler/Scheduler.php @@ -0,0 +1,88 @@ +oneTimeEvents[] = new \DateTime($dateTimeStr); + } + + /** + * add a recurring date + * + * @param string $dateTimeStr \Datetime object valid date string + */ + public function addRecurring( $dateTimeStr ) + { + $this->schedules[] = $dateTimeStr; + } + + + /** + * get next schedule date + * + * @param string $fromDateStr \Datetime object valid date string + * @return \Datetime or null + */ + public function getNextSchedule( $fromDateStr = 'now' ) + { + $dates = $this->getNextSchedules($fromDateStr, 1); + return count($dates) ? $dates[0] : null; + } + + + /** + * get a number of next schedule dates + * + * @param string $fromDateStr \Datetime object valid date string + * @param int $limit number of dates to return + * @return array + */ + public function getNextSchedules( $fromDateStr = 'now', $limit = 5 ) + { + $dates = $this->oneTimeEvents; + if (!count($this->schedules)) return $dates; + + foreach ($this->schedules as $schedule) { + $d = new \DateTime($fromDateStr); + for ($i=0; $i < $limit; ++$i) { + $dates[] = clone $d->modify($schedule); + } + } + + $this->orderDates($dates); + return array_slice($dates, 0, $limit); + } + + + /** + * @param $dates + */ + private function orderDates( &$dates ) + { + uasort($dates, function($a, $b) { + return strtotime($a->format('Y-m-d H:i:s')) > strtotime($b->format('Y-m-d H:i:s')) ? 1 : -1; + }); + } + +} \ No newline at end of file From e4ab17e13055cb09df4c967521d604d2eac08960 Mon Sep 17 00:00:00 2001 From: Nuno Date: Wed, 15 Jul 2015 17:45:42 +0100 Subject: [PATCH 3/9] readme updated --- README.md | 2 +- src/Scheduler/Scheduler.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 71a03e1..cf490f4 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Include jupitern/datatables in your project, by adding it to your composer.json ## Usage ```php // instance Scheduler -$schedules = \Lib\Dispatcher\Scheduler::instance() +$schedules = \Jupitern\Scheduler::instance() // add a one time event date // accepts any string compatible with php DateTime object diff --git a/src/Scheduler/Scheduler.php b/src/Scheduler/Scheduler.php index 556e6ca..b56a210 100644 --- a/src/Scheduler/Scheduler.php +++ b/src/Scheduler/Scheduler.php @@ -2,6 +2,12 @@ namespace Jupitern\Table; +/* + * Simple Scheduler class + * + * Author: Nuno Chaves + * */ + class Scheduler { private $schedules = array(); From 14aab89c75438a0da5f2a1cdd209cfaad9131849 Mon Sep 17 00:00:00 2001 From: Nuno Date: Wed, 15 Jul 2015 17:47:06 +0100 Subject: [PATCH 4/9] readme updated --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cf490f4..b7e2bff 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,6 @@ output: ## License -jupitern/table is release under the MIT license. +jupitern/scheduler is release under the MIT license. You are free to use, modify and distribute this software, as long as the copyright header is left intact From df74d85e1709f3b603b75a78d1d04254c22b2d16 Mon Sep 17 00:00:00 2001 From: Nuno Date: Wed, 15 Jul 2015 17:51:23 +0100 Subject: [PATCH 5/9] composer.json added --- composer.json | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 composer.json diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0819f4e --- /dev/null +++ b/composer.json @@ -0,0 +1,31 @@ +{ + "name" : "jupitern/scheduler", + "description": "php event schedule", + "keywords" : ["scheduler", "event"], + "homepage" : "https://github.com/jupitern/scheduler", + "license" : "MIT", + "authors" : [ + { + "name" : "Nuno Chaves", + "email" : "nunochaves@sapo.pt", + "role" : "Developer" + } + ], + "support": { + "source": "https://github.com/jupitern/scheduler", + "issues": "https://github.com/jupitern/scheduler/issues" + }, + "require" :{ + "php":">=5.4" + }, + "autoload": { + "psr-4": { + "Jupitern\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + + } + } +} \ No newline at end of file From 58437a3be6979913c690bf49f7b3049ca34d817a Mon Sep 17 00:00:00 2001 From: Nuno Date: Wed, 15 Jul 2015 18:00:48 +0100 Subject: [PATCH 6/9] namespace correction --- src/Scheduler/Scheduler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Scheduler/Scheduler.php b/src/Scheduler/Scheduler.php index b56a210..37681fd 100644 --- a/src/Scheduler/Scheduler.php +++ b/src/Scheduler/Scheduler.php @@ -1,6 +1,6 @@ Date: Wed, 15 Jul 2015 18:15:55 +0100 Subject: [PATCH 7/9] add() and addRecurring returns $this --- src/Scheduler/Scheduler.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Scheduler/Scheduler.php b/src/Scheduler/Scheduler.php index 37681fd..575ebdf 100644 --- a/src/Scheduler/Scheduler.php +++ b/src/Scheduler/Scheduler.php @@ -31,6 +31,7 @@ public static function instance() public function add( $dateTimeStr ) { $this->oneTimeEvents[] = new \DateTime($dateTimeStr); + return $this; } /** @@ -41,6 +42,7 @@ public function add( $dateTimeStr ) public function addRecurring( $dateTimeStr ) { $this->schedules[] = $dateTimeStr; + return $this; } From 73d3a45e74d088f80bf01b131b73c857b1665a4a Mon Sep 17 00:00:00 2001 From: Nuno Chaves Date: Mon, 15 Feb 2016 12:28:11 +0000 Subject: [PATCH 8/9] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b7e2bff..72a2f4e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ PHP 5.4 or higher. ## Installation -Include jupitern/datatables in your project, by adding it to your composer.json file. +Include jupitern/scheduler in your project, by adding it to your composer.json file. ```javascript { "require": { @@ -63,4 +63,4 @@ output: jupitern/scheduler is release under the MIT license. -You are free to use, modify and distribute this software, as long as the copyright header is left intact +You are free to use, modify and distribute this software From 47ba5e8368b0b9d115957248417dca6ac84c9907 Mon Sep 17 00:00:00 2001 From: Nuno Chaves Date: Tue, 16 Feb 2016 15:51:35 +0000 Subject: [PATCH 9/9] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 72a2f4e..6a4aaf2 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Include jupitern/scheduler in your project, by adding it to your composer.json f ```javascript { "require": { - "jupitern/scheduler": "0.*" + "jupitern/scheduler": "1.*" } } ```