We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20635
    • 6 Posts
    I want to input a birthdate Tv that is in the date-picker format and then output a person’s current age as an integer into a chunk. Rounding up is also clearly a problem smiley.

    I would think that this is a job for PHP snippet, but it seems like calculating lengths of time could come from modX’s core functionality too. I’m just too green to know.

    Anybody have the answer?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 20635
        • 6 Posts
        Wow, you lead the horse to water smiley

        I am so close, but it isn’t quite working yet for me and could use another nudge:

        I have the php side working if I put in hard numbers for the birth year and month, but I haven’t been able to pass the variables over properly. After reading the wiki, I’m pretty sure it’s my lack of php training.


        I have TVs set up to create numbers for birthYear and birthMonth (days aren’t important for this use)

        I have a call in my chunk that says this:
        [tt]<a href="#">[! getAge&birthYear=`[*birthYear*]`&birthMonth=`[*birthMonth*]`!]</a>[/tt]

        And then the end of my php looks like this:
        [tt]$date_birth = (&birthYear)"-"(&birthMonth)"-01";
        $date_now = date(’Y-m-d’);

        $age = get_age($date_birth, $date_now);

        print_r($age);
        ?>[/tt]


        Can you spot the problem?
          • 4172
          • 5,888 Posts
          $date_birth = $birthYear.'-'.$birthMonth.'-01';
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 20635
            • 6 Posts
            Thanks Bruno and Susan! I had one more error by not including the "?" after the php id in my chunk. For documentation purposes, here’s the solution:

            *This calculates rough months and you would need to add a day TV if you needed exact birthdays

            1. Set up two TVs as number fields to collect birth year and month. Name them [*birthYear*] and [*birthMonth*] respectively

            2. Create a snippet called [[getAge]] and paste this code in
            <?php
            
            /* This code uses parts from http://www.php.net/manual/en/function.mktime.php#86086 and help from Susan Ottwell (http://modxcms.com/forums/index.php?action=profile;u=16) and Bruno17 (http://modxcms.com/forums/index.php?action=profile;u=14136) */
            
            function get_age($date_start, $date_end) {
                $t_lived = get_timestamp($date_end) - get_timestamp($date_start);
                $seconds_one_year = get_days_per_year($date_start, $date_end) * 24 * 60 * 60;
                $age = array();
                $age = floor($t_lived / $seconds_one_year);
                $seconds_remaining = $t_lived % $seconds_one_year;
                return $age;
            }
            function get_timestamp($date) {
                list($y, $m, $d) = explode('-', $date);
                return mktime(0, 0, 0, $m, $d, $y);
            }
            function get_days_per_year($date_start, $date_end) {
                list($y1) = explode('-', $date_start);
                list($y2) = explode('-', $date_end);
                $years_days = array();
                for($y = $y1; $y <= $y2; $y++) {
                    $years_days[] = date('L', mktime(0, 0, 0, 1, 1, $y)) ? 366 : 365;
                }
                return round(array_sum($years_days) / count($years_days), 2);
            }
            
            /* Replace the -01 with a day variable if you need it */
            
            $date_birth = $birthYear.'-'.$birthMonth.'-01';
            $date_now = date('Y-m-d');
            
            $age = get_age($date_birth, $date_now);
            
            
            print_r($age);
            ?>


            3. Then in your chunk, call this wherever you need their age:
            [!getAge? &birthYear=`[*birthYear*]`&birthMonth=`[*birthMonth*]`!]
              • 20635
              • 6 Posts
              And if anybody wants to make it easier by hooking it to a date TV, that would make my day. This is definitely a hack.