• Resolved pinkish1

    (@pinkish1)


    I have birtdate as a textline field in the database:

    dd.mm.yyyy

    I’ve tried editing the pdb-single-default template without any luck. I don’t understand how to get the current day.month.year and compute the difference between current date and birthdate.

    Anyone figured this out yet?

    The reason I didn’t set the field as “Date field” is because if I enter a date like:

    17.05.1950 and hit Save, it automatically changes to the day before:

    16.05.1950

    It does that for any date, it’s another thing I find strange ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author xnau webdesign

    (@xnau)

    The problem with the date shift is due to the difference in time between your location and the server location. Depending on the time of day, the difference puts the server on a different day. If you make sure your server is set to your local time zone, this will be less of a problem. This is done with a PHP function:

    date_default_timezone_set ( string $timezone_identifier )

    You can put that in your wp-config.php file, the timezone strings are listed here.

    Now, to calculate the difference to another date, there are several ways to do this…none of them particularly simple. You’ll need to decide how you want the difference expressed. I suggest reading this section of the PHP manual: https://php.net/manual/en/datetime.diff.php

    If that’s too technical, you can try working it just with dividing unix timestamps, but PHP may not parse your date format correctly. The idea is you convert your dates into unix timestamps (using PHP’s strtotime() function), then perform regular math, then convert the result (which will be the number of seconds difference) into whatever units you want to express the difference as.

    Thread Starter pinkish1

    (@pinkish1)

    I added this at the very end of the template, outside the loop (I’m not a programmer):

    <?php
    function CalculateAge($BirthDate) {
    list($Year, $Month, $Day) = explode("/", $BirthDate);
    $YearDiff = date("Y") - $Year;
    if(date("m") < $Month || (date("m") == $Month && date("d") < $DayDiff)) {
    $YearDiff--;
    }
    return $YearDiff;
    }
    ?>
    <?php
    $date_of_birth = '1970/03/17';
    echo 'Age: ' . CalculateAge($date_of_birth);
    ?>

    I just need to know, if you can tell me, how to enter the value from the “birthdate” field (from the database) into the $date_of_birth field instead of the hardcoded 1970/03/17.

    Plugin Author xnau webdesign

    (@xnau)

    You need to do this inside the field loop before the value is shown:

    <?php $this->field->value = CalculateAge($this->field->value); ?>

    Thread Starter pinkish1

    (@pinkish1)

    Thanks, I added your code to pdb-single-default.php inside the loop and it works:

    if ( $this->field->name == 'birthdate' ) {
    function CalculateAge($BirthDate) {
    list($Year, $Month, $Day) = explode("/", $BirthDate);
    $YearDiff = date("Y") - $Year;
    if(date("m") < $Month || (date("m") == $Month && date("d") < $DayDiff)) {
    $YearDiff--;
    }
    return $YearDiff;
    }
    $this->field->value = CalculateAge($this->field->value);
    }

    The only problem left is listing it in pdb-list-default.php, because placing the code in the same place lists a page full of errors, maybe because the function is called multiple times within the same page?

    Plugin Author xnau webdesign

    (@xnau)

    OK, don’t place the function inside your conditional, put the function at the top of the template. You only have to call the function inside of the conditional.

    put this at the top of the template:

    function CalculateAge($BirthDate) {
    list($Year, $Month, $Day) = explode("/", $BirthDate);
    $YearDiff = date("Y") - $Year;
    if(date("m") < $Month || (date("m") == $Month && date("d") < $DayDiff)) {
    $YearDiff--;
    }
    return $YearDiff;
    }

    then inside the field loop:

    if ( $this->field->name == 'birthdate' ) {
    $this->field->value = CalculateAge($this->field->value);
    }

    This will work the same way in the list template, you just have to make sure you are calling the function inside of the field loop.

    Thread Starter pinkish1

    (@pinkish1)

    Yeah, it certainly works perfectly now! Thank you so much!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Calculating age from birthdate’ is closed to new replies.