mickburkejnr
mickburkejnr

Reputation: 3690

Work out how old a user was on the 1st January using Twig and Symfony2

I'm building a system where a user's date of birth, as well as how old that user would have been on the 1st January of the year is displayed. So it would display the date of birth of Joe Bloggs as 10th October 1987, and then how old that person was on the 1st January of the current year (which would be 25 in this case).

Because the users will be displayed on a list, along with their full name and email address, I want to be able to display their age as part of this information using Twig. However, I can't seem to fathom how to achieve this.

Upvotes: 0

Views: 2703

Answers (3)

Léo Benoist
Léo Benoist

Reputation: 2541

By creating a twig extension SimpleFilter you will be able to do:{{ app.user.birthday|age }}

<?php

// src/You/YourBundle/Twig/AgeExtension.php

namespace You\YourBundle\Twig;

class AgeExtension extends \Twig_Extension
{

    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('age', array($this, 'ageCalculate')),
        );
    }

    public function ageCalculate(\DateTime $bithdayDate)
    {
        $now = new \DateTime();
        $interval = $now->diff($bithdayDate);

        return $interval->y;
    }

    public function getName()
    {
        return 'age_extension';
    }
}

Register your extension

# src/You/YourBundle/Resources/config/services.yml

You\YourBundle\Twig\AgeExtension:
    class: You\YourBundle\Twig\AgeExtension
    tags:
        - { name: twig.extension }

To use it :

{{ user.birthday|age }}

Upvotes: 4

Ahmed Siouani
Ahmed Siouani

Reputation: 13891

I think that you need to add the appropriate Twig Filter in order to turn a given Date of Birth into a specific age.

Take a look at Extending Twig part of the documentation, you may notice that adding a new filter is so trivial.

How to ...

1 - add a custom twig extension class that extends \Twig_Extension

class customExtension extends \Twig_Extension {

2 - Add the appropriate filter by overriding getFilters() method

   public function getFilters() {
        return array(
            'age' => new \Twig_Filter_Method($this, 'getAge'),
        );
    }

3 - Add some logic to get the age of a given Date of Birth

public function getAge($date) 
{
    if (!$date instanceof \DateTime) {
        // turn $date into a valid \DateTime object or let return
        return null;
    }

    $referenceDate = date('01-01-Y');
    $referenceDateTimeObject = new \DateTime($referenceDate);

    $diff = $referenceDateTimeObject->diff($date);

    return $diff->y;
}

Then, call your filter as follow,

{{ yourDateOfBirthInstance | age }}

Upvotes: 6

Amal Murali
Amal Murali

Reputation: 76636

Using DateTime class:

$januaryDate = date('01-m-Y');
$sDateBirth = '10th October 1987'; 

$oDateNow = new DateTime($januaryDate);
$oDateBirth = new DateTime($sDateBirth);
$oDateInterval = $oDateNow->diff($oDateBirth);
echo $oDateInterval->y;

Output:

25

Upvotes: 1

Related Questions