Diff years between two dates. I can not use TIMESTAMPDIFF in Query Builder

How can I calculate diff years between 2 dates using mysql function timestamdiff in my query builder? I tried to create sql custom dialect funtion but it doesn't work:

$dialect->registerCustomFunction( 'DATE_DIFF_YEARS', function ($dialect, $expression) {

    $arguments = $expression['arguments'];
    return sprintf(
        "TIMESTAMPDIFF(YEAR, %s, %s);",


I'm not sure but I don't think TIMESTAMPDIFF() is part of PHQL dialect in this case you have to add in mysql dialect, see this incubator

Good luck

What you mean it doesn't work? It should work without any problem, what exactly issue you have?