You are here

function simplenews_scheduler_calculate_next_run_time in Simplenews Scheduler 2.0.x

Same name and namespace in other branches
  1. 8 simplenews_scheduler.module \simplenews_scheduler_calculate_next_run_time()
  2. 6.2 simplenews_scheduler.module \simplenews_scheduler_calculate_next_run_time()
  3. 7 simplenews_scheduler.module \simplenews_scheduler_calculate_next_run_time()

Calculates time for the next edition to be sent.

This is set in the {simplenews_scheduler} table when a new edition is run, for subsequent cron runs to query against.

The time is strictly in the future; that is, if the $now_time is a valid edition time, a schedule interval is added to it. This is to allow for cron runs that need to calculate the next run time at the time of the current edition being sent.

Parameters

$newsletter_parent_data: A row of data from {simplenews_scheduler}, as returned by simplenews_scheduler_get_newsletters_due().

$now_time: The time of the operation.

Return value

The calculcated run time for the next future edition.

3 calls to simplenews_scheduler_calculate_next_run_time()
SimplenewsSchedulerNextRunTimeTest::testNextRunTimeOneMonth in tests/src/Functional/SimplenewsSchedulerNextRunTimeTest.php
Test a frequency of 1 month.
SimplenewsSchedulerNextRunTimeTest::testNextRunTimeTwoMonths in tests/src/Functional/SimplenewsSchedulerNextRunTimeTest.php
Test a frequency of 2 months.
simplenews_scheduler_scheduler_update in ./simplenews_scheduler.module
Updates a scheduler record with any housekeeping changes and saves it.

File

./simplenews_scheduler.module, line 430
Simplenews Scheduler module allows a schedule to be set for sending (and resending) a Simplenews item.

Code

function simplenews_scheduler_calculate_next_run_time($newsletter_parent_data, $now_time) {

  // Make an offset string of the format '+1 month'.
  $offset_string = _simplenews_scheduler_make_time_offset($newsletter_parent_data->send_interval, $newsletter_parent_data->interval_frequency);

  // Make a DateInterval object that represents this.
  $date_interval = DateInterval::createFromDateString($offset_string);

  // Create a date object to act as a pointer we'll advance and increment.
  if ($newsletter_parent_data->last_run) {

    // Generate a date string to initialize a DateTime() object, otherwise the
    // timezone is ignored.
    $start_date = date('Y-m-d H:i:s', $newsletter_parent_data->last_run);
  }
  else {
    $start_date = date('Y-m-d H:i:s', $newsletter_parent_data->start_date);
  }

  // Initialize the DateTime object using the configured ste timezone.
  $pointer_date = new DateTime($start_date);

  // Add as many offsets as possible until we get into the future.
  while ($pointer_date
    ->getTimestamp() <= $now_time) {

    // Add interval to the pointer time.
    $pointer_date
      ->add($date_interval);
  }
  return $pointer_date
    ->getTimestamp();
}