How to show a user's age at the time of creating a node in a View?

by Jeroen   Last Updated January 05, 2018 21:07 PM

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.

Tags : views datetime


Answers 1


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" ]
    }
  }
]

} }

pinueve
pinueve
January 05, 2018 20:57 PM

Related Questions


How to show a date in the correct timezone?

Updated February 27, 2017 20:07 PM

Views timestamp not recognized as date?

Updated March 07, 2017 01:07 AM


How to add current date to a view in drupal 8

Updated November 15, 2017 11:07 AM