You are here

function simplenews_scheduler_calculate_edition_time in Simplenews Scheduler 8

Same name and namespace in other branches
  1. 6.2 simplenews_scheduler.module \simplenews_scheduler_calculate_edition_time()
  2. 7 simplenews_scheduler.module \simplenews_scheduler_calculate_edition_time()
  3. 2.0.x simplenews_scheduler.module \simplenews_scheduler_calculate_edition_time()

Calculates time for the current edition about to be created.

Because cron may run after the scheduled timestamp, one or more scheduled edition times may have been skipped. This calculates the most recent possible time for an edition.

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 creation time of the newsletter edition.

5 calls to simplenews_scheduler_calculate_edition_time()
SimplenewsSchedulerDaylightSavingSwitchTest::testDSTMonthly in src/Tests/SimplenewsSchedulerDaylightSavingSwitchTest.php
Test edition time after DST changes for a monthly newsletter.
SimplenewsSchedulerEditionDueTest::testEditionsDue in src/Tests/SimplenewsSchedulerEditionDueTest.php
Test simplenews_scheduler_get_newsletters_due().
SimplenewsSchedulerEditionTimeTest::testEditionTimeOneMonth in src/Tests/SimplenewsSchedulerEditionTimeTest.php
Test a frequency of 1 month.
SimplenewsSchedulerEditionTimeTest::testEditionTimeTwoMonths in src/Tests/SimplenewsSchedulerEditionTimeTest.php
Test a frequency of 2 months.
simplenews_scheduler_cron in ./simplenews_scheduler.module
Implements hook_cron().

File

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

Code

function simplenews_scheduler_calculate_edition_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);

  // Take the last run time and add as many intervals as possible without going
  // past 'now'.
  // 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);
  while ($pointer_date
    ->getTimestamp() <= $now_time) {

    // Get the last iteration's timestamp before we change the pointer.
    $timestamp_old = $pointer_date
      ->getTimestamp();

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

    // Check if the pointer is now in the future.
    if ($pointer_date
      ->getTimestamp() > $now_time) {

      // If so, return the last iteration timestamp as the edition time.
      return $timestamp_old;
    }
  }
}