简体   繁体   English

TIMESTAMPDIFF-如何在PHP / MySQL中使用它来计算日期值之间的差异

[英]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. 上面的链接帮助我如何计算两个日期之间的差异,但是, 我需要在运行时通过PHP中的MySQL select查询在特定列上执行相同的操作。

For example: 例如:

Profile table: 资料表:

  • Name 名称
  • Gender 性别
  • DOB (Date of Birth) DOB(出生日期)

Running following query via MySQL console, gives exactly the needed result: 通过MySQL控制台运行以下查询,将给出所需的结果:

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: 但是,我需要完成以下查询,以使我获得相同的结果,并结合整个DOB值的其他条件:

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. 然后,您将在结果集中找到一个额外的字段age

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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