Calculate age in Months/Day...

HyperOsmar

Member
Dear colleagues, I need your help...

I have a Date Element where the user enters the date of birth.
Just below another Calc Element that calculates the age of the user. It works perfectly.
The point is that the form is for registering premature children (neonatal) and I need the age to be informed not in years, but in months or days (if the children have not yet completed one month).
I'm using the following code:

$date= '{cad_telemonitoramento_2fase___datanascimento}';
$date = strtotime($date);
$now = time();
$diff = $now - $date;
$years = floor($diff / 31556926);
return $years;
 
Thanks @troester e @lousyfool,
But I need that instead of just the numeral, the result is shown as follows:
<= 30 days (23 d);
> 30 days (01 m and 15 d)...
It's possible???...
 

Attachments

  • tela_001.png
    tela_001.png
    15 KB · Views: 43
  • tela_002.png
    tela_002.png
    19.5 KB · Views: 41
@lousyfool thanks for the tip...
I'm not a programmer, I even tried using your tips and nothing...
By day and month I got it, but because it is neonatal (newborns) the most correct thing is for the Calc element to show the result for days/weeks, and not days/months as I had requested...
Is it possible for Calc to show me the result in days/weeks format as an example below???...

https://pt.calcuworld.com/calculadorasdegravidez/idade-corrigida-prematuro/
 
Last edited:
Dear colleagues,

I came to this code, apparently it's working as I wanted but, as I'm not a programmer, can you check if it's correct or if it's possible to improve?

$dn = '{cad_telemonitoramento_2fase___idade_gestacional_corrigida}';
$dn = date('Y-m-d', strtotime($dn));
$today = date('Y-m-d');
$difference = strtotime($today) - strtotime($dn);
$difference = floor($difference / (60 * 60 * 24));
$weeks = intdiv($difference, 7);
$days = $difference - ($weeks * 7);
return $weeks ." Semanas e ". $days ." Dias";
 
Not Fabrik, only PHP and maybe UX. So, you should really "shop" elsewhere for verification or help with this. ;)

However, just out of personal interest: IMO your math could be slightly simplified, and a few extra lines of code should be invested into the output. Perhaps like this:
Code:
$dn = '{cad_telemonitoramento_2fase___idade_gestacional_corrigida}';
$difference_secs = time() - strtotime($dn);
$difference_days = round($difference_secs / 86400, 0);
$weeks = intdiv($difference_days, 7);
$days = $difference_days - ($weeks * 7);
if ($difference_days < 0) {
    return "The date you entered is in the future.";
} elseif ($difference_days == 0) {
    return "No significant difference.";
} else {
    if ($weeks == 0) {
        $weeks_display = "";
    } elseif ($weeks == 1) {
        $weeks_display = "1 Semana";
    } else {
        $weeks_display = $weeks." Semanas";
    }
    if ($days == 0) {
        $days_display = "";
    } elseif ($days == 1) {
        $days_display = "1 Dia";
    } else {
        $days_display = $days." Dias";
    }
    if ($weeks > 0 && $days > 0) {
        $and_display = " e ";
    } else {
        $and_display = "";
    }
    return $weeks_display.$and_display.$days_display;
}
You see, in my math I also used round() instead of floor(), because otherwise you'll sometimes "lose" a day and it'd be inaccurate.
Anyway, whichever code or modification of it you use: if it works for you, then so be it, and here it's "case closed".
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top