For some of my node types concerning the week schedule of our organisation, the node should contain the dates of each day of the week. Of course I can create seven fields and have the end user fill them in when creating the node. It would be a lot easier, however, if it would enough for him to enter only the date of monday (the first day in my week) and let the other fields be calculated based on this one (tuesday = monday + 1 day, wednesday = monday + 2 days, etc...).

So I have a cck field "datema" in which the end user has to enter the date of a monday. I then have another date field "datedi" and a rule containing the following elements:
- ON event Content is going to be saved
- IF Saved content is Rooster week 5
- DO Populate saved content's field 'field_datedi'

In the last action (Populate saved content's field 'field_datedi') I have entered the following code (based upon what I've gathered from this forum) under "Advanced: Specify the fields value with PHP code":

return array( 0 => array('value' => date('Y-m-d', $node->field_datema[0]['timestamp'] + 1 * 86400)));

When I create a new node of the type "Rooster week 5", enter a value in the field datema and save it, the result I get for the field datedi is 02/01/1970. Instead of adding 1 day to the value of datema, it adds 1 day to a given start date of 01/01/1970!

How can I solve this? What php code can I enter in order to populate my field datedi with the correct date?

Comments

2by’s picture

this is what i did

$date = date("Y-m-d");
return array( 0 => array('value' => strtotime(date("Y-m-d", strtotime($date)) . " +10 day")));

hope it helps

2by’s picture

opps my bad ,

$date = date("Y-m-d");
return array( 0 => array('value' => strtotime(date("Y-m-d", strtotime($date)) . " +10 days")));

more example

strtotime($date)) . " +1 day"
strtotime($date)) . " +1 week"
strtotime($date)) . " +2 week"
strtotime($date)) . " +1 month"
strtotime($date)) . " +30 days"

SuperStes’s picture

Thanks for the reply, but I can't see where you get data from another CCK field in your code.

Am I correct to presume that your output is the current date + 10 days? (BTW: when I enter your code as a test, I just get the current date as output. No days are added!)

What I am looking for, is a way to save the date of a given CCK date field + 1 day in another CCK date field. Still haven't found a solution though ...

Wallack’s picture

This is how I solved it:

1 .- Enable the PHP filter.
2 .- Add new action (PHP: Custom PHP code (or something like that)).

There you will see the available variables like: $node, $node_added ...

And what I did:

$node_added->field_date_event[0]['value'] = $node->field_date[0]['value'];

Works perfectly with taxonomies and a lot of things and solved a lot of problems to me.

Kind regards.

itangalo’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

landing’s picture

You don't need to set that $date variable, since the php date and strtotime function is grabbing the current date, you can get the current date and any past or future date like so:

return array( 0 => array('value' => date('Y-m-d', strtotime('+8 days')))
);

Encarte’s picture

Category: support » bug
Status: Closed (fixed) » Active

I reproduced a similar situation and none of the solutions worked. Can someone actually confirm that any of those worked?

Additional, if none of this solutions works, then this is not a php problem but some kind of bug. I'm unable to change any date module field with rules using the php advanced feature.

Encarte’s picture

Category: bug » support
Status: Active » Closed (fixed)

Sorry, #7 does work, I was having a php problem unrelated to this issue.

SuperStes’s picture

Sorry, my problem still isn't fixed.

Wallack, I implemented your code and it works fine to copy the date from one field to another. Thanks! Here's what I entered under the "execute custom PHP code" action:

$node->field_datedi[0]['value'] = $node->field_datema[0]['value'];

So when I add a node and fill up the "datema"-field (being the date of monday), it automaticly fills up the "datedi"-field (being the date of tuesday) with the same date. However, I need it to be +1 day!.

When I alter the PHP code, it gives an error most of the times, or still yields the exact same date as that of the "datema"-field, or invariably yields 01/01/2011.

I guess i'm just in way over my head here and just gonna give up trying to make it work...

@landing: just like 2by, you seem to be adding days to the current date, which is not what i'm looking for! I have done stuff like that on my website and that is working fine. It's adding a given number of days to the date of one field and saving that new, calculated date in another field of the same node.

mitchell’s picture

Component: Miscellaneous » Rules Core

Updated component.

SuperStes’s picture

Status: Closed (fixed) » Closed (won't fix)
baseconversion’s picture

Jan_MSK did you ever figure out how to do it?

SuperStes’s picture

No, never found a way to make it work :-(