4

I have a rules component that sends me an e-mail containing a view that contains certain statistics about my site.

I'd like to have this e-mail sent at midnight every day.

The rule looks like this:

Component: Action set

  1. Send e-mail
  2. Schedule the component again for evaluation (+1 day)

I then triggered the rule manually at midnight and waited for it to evaluate again. This worked for the first several days but now the e-mail is arriving at 3 or 4 in the morning instead of at midnight. I really want the e-mail to be sent at the same time every day (or as close to the same time as possible every day). Is there a way to do this?

2

To finally answer the original question in the title, you can run a rule at a specific hour of the day by writing for example

today + 9 hour

"today" is always the actual day at 0:00:00, "+ X hour" adds any number of hours to this. Note that "hour" is written in singular. Other keywords could be "month", "week", "day", "minute" or "second". You can combine keywords. There's a lot more, refer to this page for further details:

http://php.net/manual/en/datetime.formats.relative.php

5

How about this:

When you're setting the time in the "Scheduled Evaluation Date" section, change it from 'Direct input' to 'Selection mode', and scroll down to where it gives you the option for 'PHP Evalution'

Paste in the following:

<?php
$midnight = strtotime('midnight');
echo date("Y-m-d h:i:s", $midnight);
?>

This should return something like '2013-03-25 12:00:00' // That's what it's returning for today!

You have to include the php brackets. The strtotime() function should return back midnight of the current day, so it should work for you. I'm thinking since this is based on your cron running, it will not be exact and this should evaluate a little past midnight and 'midnight of the current' day should be exactly what you need. Maybe you can find a php tweak that makes this better...

Anyway, let us know if that works!

  • 1
    Actually, I didn't even need to use PHP here. Based on your suggestion, I set the Scheduled evaluation date to midnight +1 day and that did the trick. – Patrick Kenny Mar 28 '13 at 15:01
  • Oh yes, of course. That makes sense! :-) – Boriana Ditcheva Mar 28 '13 at 15:24
2

I've written a module that might do what you want more reliably:

https://www.drupal.org/project/rules_onceperday

The rule event is triggered by cron, but you can specify which hour of the day the event occurs, and it will be run the first time cron runs after that hour.

As well as specifying which hour of the day the event triggers after, there are also day-of-the-week conditions to allow you to specify which days the event can run on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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