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
Comment #1
2by commentedthis 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
Comment #2
2by commentedopps 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"
Comment #3
SuperStes commentedThanks 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 ...
Comment #4
Wallack commentedThis 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.
Comment #5
itangalo commentedComment #7
landing commentedYou 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')))
);
Comment #8
Encarte commentedI 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.
Comment #9
Encarte commentedSorry, #7 does work, I was having a php problem unrelated to this issue.
Comment #10
SuperStes commentedSorry, 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.
Comment #11
mitchell commentedUpdated component.
Comment #12
SuperStes commentedComment #13
baseconversion commentedJan_MSK did you ever figure out how to do it?
Comment #14
SuperStes commentedNo, never found a way to make it work :-(