Subtracting dates in PHP(在 PHP 中减去日期)
问题描述
可能重复:
如何使用 PHP 计算两个日期之间的差异?
我有以 YYYY-MM-DD HH:MM:SS
格式存储的时间戳(例如 2010-06-21 20:12:56
).检查时间戳有多旧的最佳方法是什么?目前我主要关心的是天数.
I have timestamps stored in the format YYYY-MM-DD HH:MM:SS
(for example 2010-06-21 20:12:56
). What would the best way to check how old the timestamp is? For the moment I am mainly interested in the number of days old.
推荐答案
你可以使用 strtotime
将字符串转换为 UNIX 时间戳,以秒为单位.time()
将为您提供当前的 UNIX 时间戳.减去它们得到日期的多少秒,然后除以 60*60*24
得到它的天数
You can use strtotime
to convert the string to a UNIX timestamp, which is in seconds. time()
will give you the current UNIX timestamp. Subtract them to get how old the date is in seconds, and divide by 60*60*24
to get it in days
使用 DateTime::diff 也是可行的,虽然我发现日期函数比使用类更容易
It's also doable using DateTime::diff, although I find the date functions easier than using the classes
这篇关于在 PHP 中减去日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中减去日期


基础教程推荐
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01