phpmysqldatetimetimestampdiff

TIMESTAMPDIFF - How to use it in PHP/MySQL to calculate difference between date values


TIMESTAMPDIFF

The link above have helped me how to calculate difference in two dates, however, I need to do the same on a particular column at run time via MySQL select query inside PHP.

For example:

Profile table:

Running following query via MySQL console, gives exactly the needed result:

SELECT TIMESTAMPDIFF(YEAR, DOB, CURDATE()) AS Age FROM profile;

However, I need to complete the following query to get me the same result combined with other conditions on whole set of DOB values:

SELECT * FROM Profile WHERE Gender='$gn';

I checked Sub-query but that, wont work due to more than one return value.


Solution

  • As you said, it's the same table, you could use this query:

    SELECT *,TIMESTAMPDIFF(YEAR, DOB, CURDATE()) AS Age
    FROM Profile 
    WHERE Gender='$gn';
    

    Then you'll find an additional field age in your result set.