gpt4 book ai didi

php - 计算两个日期之间的天数

转载 作者:行者123 更新时间:2023-11-29 03:52:51 24 4
gpt4 key购买 nike

我有一个 mySQL 数据库。

我需要计算两个日期之间的天数。

我的客户将通过 php 表单将输入 hm_date 填写为 1979 年 1 月 1 日,以创建新记录。

我需要一个字段 total_days 来计算从 hm_date 到今天的总天数。我需要这个字段每天都更新。

如何使 hm_date 与总天数一起显示并始终更新?

我想这可以在服务器端实现吗?

我应该使用 strototime() 吗?

最佳答案

您需要使用 MySQL 的 DATEDIFF()

DATEDIFF() returns expr1 – expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');
-> 1
mysql> SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31');
-> -31

根据您的问题,我认为您需要 DATE_DIFF(hm_date, CURRENT_DATE)。只需确保 hm_dateYYYY-MM-DD 格式即可。

关于php - 计算两个日期之间的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10543883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com