12

I'm building a module but am stuck.. I need the timestamp from the previous cron as I run a hook_cron job so I can check which nodes are new and email those.

So I need the date/time stamp from the last cron run but how do I get there'?

2 Answers 2

20

A UNIX timestamp of the last cron run can be fetched with:

variable_get('cron_last');

If you need to, you can easily manipulate a UNIX timestamp with PHP's date function.

0
3

hook_requirement() function can help you.

Check this out: http://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_requirements/7

In your module file write hook_requirement() function.

function hook_requirements($phase) {
if ($phase == 'runtime') {
    $cron_last = variable_get('cron_last');

    if (is_numeric($cron_last)) {
      $requirements['cron']['value'] = $t('Last run !time ago', array('!time' => format_interval(REQUEST_TIME - $cron_last)));
    }
    else {
      $requirements['cron'] = array(
        'description' => $t('Cron has not run. It appears cron jobs have not been setup on your system. Check the help pages for <a href="@url">configuring cron jobs</a>.', array('@url' => 'http://drupal.org/cron')), 
        'severity' => REQUIREMENT_ERROR, 
        'value' => $t('Never run'),
      );
    }

    $requirements['cron']['description'] .= ' ' . $t('You can <a href="@cron">run cron manually</a>.', array('@cron' => url('admin/reports/status/run-cron')));

    $requirements['cron']['title'] = $t('Cron maintenance tasks');
  }
}
2
  • helpfull but I like bart's answer better because it's simpler +1
    – FLY
    Commented Sep 14, 2012 at 13:18
  • hook_requirements() only gets invoked for the "runtime" phase from the status report page callback (cf, api.drupal.org/api/drupal/modules%21system%21system.api.php/…). I don't see how this would help determine which nodes are new.
    – mpdonadio
    Commented Sep 14, 2012 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.