Skip to content

Commit

Permalink
- Changed method add to allow relative one time events like "+1 hour"…
Browse files Browse the repository at this point in the history
… or "next day 17:00"

- relative events added with method add() are relative to $fromStartDate and obey time frame if set
  • Loading branch information
Nuno committed Dec 16, 2016
1 parent a79d862 commit 61314e6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,33 @@ output:
2030-01-02 12:00
*/


$schedules = \Jupitern\Scheduler\Scheduler::instance()
->setTimeFrame('08:30', '19:00')
->add('+10 minutes')
->add('+30 minutes') // outside time frame. will not produce any schedule
->add('next day 08:30')
->getNextSchedules('2000-12-16 18:40');

foreach ($schedules as $schedule) {
echo $schedule->format('Y-m-d H:i')."<br/>";
}

/*
output:
2000-12-16 18:50
2000-12-17 08:30
*/

```

## ChangeLog

v1.3

- Changed method add to allow relative one time events like "+1 hour" or "next day 17:00"
- relative events added with method add() are relative to $fromStartDate and obey time frame if set

## Contributing

- welcome to discuss a bugs, features and ideas.
Expand Down
36 changes: 25 additions & 11 deletions src/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public static function instance()
/**
* set a time frame in which events will occur
*
* @param string $startTime \Datetime start time string compatible with php Datetime class. example: '08:00'
* @param string $endTime \Datetime end time string compatible with php Datetime class. example: '17:00'
* @param string $startTime time string compatible with \Datetime object. example: '08:00'
* @param string $endTime time string compatible with \Datetime object. example: '17:00'
*/
public function setTimeFrame( $startTime = null, $endTime = null )
{
Expand All @@ -46,18 +46,18 @@ public function setTimeFrame( $startTime = null, $endTime = null )
/**
* add a one time occurring date
*
* @param string $dateTimeStr \Datetime object valid date string
* @param string $dateTimeStr date string compatible with \Datetime object
*/
public function add( $dateTimeStr )
{
$this->oneTimeEvents[] = new \DateTime($dateTimeStr);
$this->oneTimeEvents[] = $dateTimeStr;
return $this;
}

/**
* add a recurring date
*
* @param string $dateTimeStr \Datetime object valid date string
* @param string $dateTimeStr date string compatible with \Datetime object
*/
public function addRecurring( $dateTimeStr )
{
Expand All @@ -69,7 +69,7 @@ public function addRecurring( $dateTimeStr )
/**
* get next schedule date
*
* @param string $fromDateStr \Datetime object valid date string
* @param string $fromDateStr date string compatible with \Datetime object
* @return \Datetime or null
*/
public function getNextSchedule( $fromDateStr = 'now' )
Expand All @@ -82,17 +82,20 @@ public function getNextSchedule( $fromDateStr = 'now' )
/**
* get a number of next schedule dates
*
* @param string $fromDateStr \Datetime object valid date string
* @param string $fromDateStr date string compatible with \Datetime object
* @param int $limit number of dates to return
* @return array
*/
public function getNextSchedules( $fromDateStr = 'now', $limit = 5 )
{
$dates = [];

foreach ($this->oneTimeEvents as $evt) {
if ($this->isInTimeFrame($evt, $fromDateStr)) {
$dates[] = $evt;
foreach ($this->oneTimeEvents as $schedule) {
$dt = new \DateTime($fromDateStr);
$dt->modify($schedule);

if ($this->isInTimeFrame($dt, $fromDateStr)) {
$dates[] = $dt;
}
}

Expand All @@ -101,7 +104,7 @@ public function getNextSchedules( $fromDateStr = 'now', $limit = 5 )

for ($i=0, $maxRecursion = 100 * $limit; $i < $limit && $maxRecursion > 0; ++$i, --$maxRecursion) {

if (strpos($schedule, '+') !== false) {
if ($this->isDateRelative($schedule)) {
if ($this->startTime instanceof \DateTime && $d < $this->startTime->modify($d->format('Y-m-d'))) {
$d->modify($this->startTime->format('H:i:s'));
}
Expand All @@ -128,6 +131,7 @@ public function getNextSchedules( $fromDateStr = 'now', $limit = 5 )

/**
* @param array $dates
* @return array
*/
private function orderDates( &$dates )
{
Expand All @@ -138,6 +142,7 @@ private function orderDates( &$dates )

/**
* @param \DateTime $date
* @return bool
*/
private function isInTimeFrame(\DateTime $date, $fromDateStr = 'now')
{
Expand All @@ -155,4 +160,13 @@ private function isInTimeFrame(\DateTime $date, $fromDateStr = 'now')
return true;
}

/**
* @param string $dateStr \Datetime object valid date string
* @return bool
*/
private function isDateRelative($dateStr)
{
return strpos($dateStr, '+') !== false;
}

}

0 comments on commit 61314e6

Please sign in to comment.