I want to set a rule which checks a date of format "2014-09-24 08:43:39" is a Monday using the data comparison rule. I have tried to manipulate the data selector for the date using the ":custom:N" format however data comparison wont accept it. I'm trying to do it from within the rules module as my PHP is a bit limited. Any ideas? Thanks Adrian

2 Answers 2


I had myself many troubles with Rules and dates too. There are several open issues in Rules about dates. So the solution that I offer is very cumbersome and to be honest I don't like it too, but it seems to be the only thing to work. Improvements are of course welcome. Maybe there is also some module which I miss.

Note that this is about Rules ver. 7.x-2.7. The solution involves both some PHP code and components.

So the first issue at hand is that it seems Rules cannot change the format of a date variable in a data comparison, or to properly convert the data type.

Therefore I am using a rule to get the weekday from the variable, then pass it to a component as a variable (integer). In that component I can easily do a data comparison for the interger and procedd with any actions.

In order to get the weekday, I create an action "Add a variable". In there, I use some PHP code in order to directly get the week day. Here is the second issue: Rules do not seem to properly expose the variables in PHP. Therefore I specify the type of the variable as text, and then I specify the value using one of the replacement patterns in the direct input mode (otherwise a date will not be available in the data selection mode). Now we have the date as a string in a variable. Let name the variable variable_added.

The second action is to "Set a data value". In there, you specify:

$datetime = new DateTime(DateTime::createFromFormat('D, d/m/Y - H:i', $variable_added));
return $datetime->format('w');

We create a DateTime object from a hard-coded format. This has to be the Drupal long date format which Rules seem to use for their date formatting. There got to be an API function that returns the current format which atm I miss, and so we have it hard coded to get us going. Replace this with your installation's custom format from /admin/config/regional/date-time, also consult this. Then we use DateTime::format to get the day number.

So now we have the day number as a string. The third action would be to convert the above variable to an integer, and then in a fourth action you would call the rules component passing on the converted variable along with any other variables you would need.

Appendix: My test case rule export.

{ "rules_test_date" : {
    "LABEL" : "Test date",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "php" ],
    "ON" : { "cron" : [] },
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "[site:current-date]" },
          "PROVIDE" : { "variable_added" : { "variable_added" : "Added variable" } }
      { "data_set" : {
          "data" : [ "variable-added" ],
          "value" : "\u003C?php\r\n$datetime = new DateTime(DateTime::createFromFormat(\u0027D, d\/m\/Y - H:i\u0027, $variable_added));\r\nreturn $datetime-\u003Eformat(\u0027w\u0027);\r\n?\u003E"
      { "drupal_message" : { "message" : [ "variable-added" ] } },
      { "data_convert" : {
          "USING" : { "type" : "integer", "value" : [ "variable-added" ] },
          "PROVIDE" : { "conversion_result" : { "conversion_result" : "Conversion result" } }
  • Thankyou @Wtower for your great work, really appreciate it. I can follow you up until the "set a data value" part, In the rule, I add an action "set a data value" with Selected data: variable-added, and in the value box, I paste the php code. I think this is where I am going wrong as if I do a message on the site with the "variable-added" included, I get the php code printed out.
    – Adrian
    Commented Sep 25, 2014 at 9:48
  • If I print out the "variable-added" before applying the "set data value" the date format shown has the 3 letter weekday format at the start, if that helps. Thanks Adrian
    – Adrian
    Commented Sep 25, 2014 at 9:51
  • You're welcome, Adrian. Can you verify that the PHP module is enabled in the list of modules?
    – Wtower
    Commented Sep 25, 2014 at 10:25
  • If you mean the PHP filter module, no it wasn't, now it is and I get:"<br /> <b>Parse error</b>: syntax error, unexpected T_VARIABLE in <b>C:\Users\Adrian\Documents\My Web Sites\acquia-drupal7\sites\all\modules\rules\modules\php.eval.inc(125) : eval()'d code</b> on line <b>3</b><br /> " as a result.
    – Adrian
    Commented Sep 25, 2014 at 12:31
  • It seems that this error comes from the PHP code in the action, but I can't see something wrong with the code. It runs ok to me. Can you import the test rule and give it a shot by running cron?
    – Wtower
    Commented Sep 25, 2014 at 12:37

Don't know if this works for your specific case, but for me this little module is quite useful in a similar context as it provides for a "check what day it is" Rules condition (and Event):


The module also provides a "Day of the week is a" Rules condition, to further restrict actions to being run on specific day(s) of the week, on working days only, or at weekends only.

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.