0

My users have a Date of birth field (I am using the Date module). Every node page contains a View with some information about the node. I would like to add the author's age at the time of publication to it. How can I do that, assuming that Date of birth is available as a Views field?

I first tried to use Time Ago, but unfortunately it is not possible to select a time range. Next, I computed the difference between the time of publication and the date of birth in Unix seconds with a mathematical expression, but I am not sure how to convert this value to a year.

2 Answers 2

2

it can be done by rules, here is a rule that calculate in DAYS the difference between two dates. my rules does the following, make necesary changes to fit your needs: First convert both dates to an integer value, then substract them (date1-date2= diff-value in seconds), for days value divide diff-value by 86400 (24*60*60), this new variable convert it to an integer, this last value is the diference in days between those 2 dates. react on after saving(updating) your node, store this value by rules in a node hidden field, last add this field to your view.

rule's code:

{ "rules_parcialidad_calculate_dias_de_retraso" : {
"LABEL" : "parcialidad calculate dias de retraso",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"TAGS" : [ "calculate retraso pago" ],
"REQUIRES" : [ "rules" ],
"ON" : { "node_update--parcialidad" : { "bundle" : "parcialidad" } },
"DO" : [
  { "data_convert" : {
      "USING" : {
        "type" : "integer",
        "value" : [ "node-unchanged:field-fecha-de-parcialidad" ]
      },
      "PROVIDE" : { "conversion_result" : { "fecha_parcialidad_converted" : "fecha_parcialidad_converted" } }
    }
  },
  { "data_convert" : {
      "USING" : { "type" : "integer", "value" : [ "node:field-fecha-de-pago" ] },
      "PROVIDE" : { "conversion_result" : { "fecha_pago_converted" : "fecha_pago_converted" } }
    }
  },
  { "data_calc" : {
      "USING" : {
        "input_1" : [ "fecha-pago-converted" ],
        "op" : "-",
        "input_2" : [ "fecha-parcialidad-converted" ]
      },
      "PROVIDE" : { "result" : { "diferencia_en_segundos" : "direfencia_en_segundos" } }
    }
  },
  { "data_calc" : {
      "USING" : {
        "input_1" : [ "diferencia-en-segundos" ],
        "op" : "\/",
        "input_2" : "86400"
      },
      "PROVIDE" : { "result" : { "diferencia_en_dias" : "diferencia_en_dias" } }
    }
  },
  { "data_convert" : {
      "USING" : { "type" : "integer", "value" : [ "diferencia-en-dias" ] },
      "PROVIDE" : { "conversion_result" : { "retraso_dias_round" : "retraso_dias_round" } }
    }
  },
  { "data_set" : {
      "data" : [ "node:field-parcialidades-dias-retraso" ],
      "value" : [ "retraso-dias-round" ]
    }
  }
]

} }

3
  • This is a possible solution. Bear in mind though that storing the result in a separate field might raise issues when a user changes the date of birth later on (suppose the date was wrong): all stored values should then be updated.
    – Jeroen
    Jan 5, 2018 at 21:32
  • 1
    your own answer is based on mine, if you wish, please upvote my answer
    – pinueve
    Jan 5, 2018 at 22:10
  • 1
    I had actually figured it out myself in the meantime, but the general approach is indeed similar.
    – Jeroen
    Jan 5, 2018 at 23:36
0

Okay, this was actually really simple: divide the difference in Unix seconds between the day of publication and the day of birth by the number of seconds in a year (31536000 is used in the Time Ago API), and floor the result. The mathematical expression in the Views field thus becomes

([created] - [field_date_of_birth]) / 31536000

where rounding is enabled. Mind that both date fields use the custom format U.

One more caveat: the HTML should be removed from the Date field Date of birth. Otherwise, its value will be considered as 0 and the result will be wrong.

Your Answer

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

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