简体   繁体   中英

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:

  • Name
  • Gender
  • DOB (Date of Birth)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM