php - 如何理解 php 中的一个日期是否小于另一个减去一天?
问题描述
如何理解 php 中的一个日期是否小于另一个减去一天?我的意思是,例如,如果将日期设置为“2018/07/03 ”;我如何理解给定日期是否小于“ 2018/07/02 ”
date1 : year1/month1/day1 date2: year2/month2/day2
<?php
if ($year1 >= $year2) {
if ($month1 >= $month2) {
if (($day1 - 1) > $day2) {
echo 'you could do something..';
}
}
}
?>
如果例如 $year2 = 2017 和 $month2 = 11 .. 任何人都可以帮助我,上面的代码将失败?多谢..
解决方案
在这里,这应该工作。
$date_to_check = new DateTime($yesterday);
$today = new DateTime();
$time_diff = $today->diff($date_to_check)->d;
if($time_diff > 1) {
echo "This is greater than one day.";
}else{
echo "This is not greater than one day.";
推荐阅读
- javascript - 当后续字符可能不同时,在自定义 JS 变量中使用正则表达式来提取字符串的一部分
- python - 查找在 8 天内具有最大平均值的行
- c# - 在超链接字段 href 中设置下拉列表值
- javascript - 导入 Office ui/Bootstrap 时出现“Uncaught SyntaxError: Unexpected token export”
- javascript - Electron:如何安全地将全局变量注入 BrowserWindow / BrowserView?
- postgresql - Neo4j 关系属性索引的最佳实践?
- kubernetes - k8s 中的哪个 API 组
- linux - 如何更新 minikube 最新版本?
- javascript - 计算动态创建的 Div 元素和所有子元素的加载时间
- d3.js - d3.axis.ticks() 将被忽略