gpt4 book ai didi

php - 检查日期是否比当前日期更早或更新(按月)

转载 作者:行者123 更新时间:2023-12-04 20:33:59 25 4
gpt4 key购买 nike

我想检查给定日期(如 2012-12)是否早于或晚于当前日期。

我知道如何查看旧月份

if(strtotime('2012-12')<strtotime('-1 Months')){
echo "true"; // got TRUE ... correct!
} else {
echo "false";
}

但是更新的呢?

if(strtotime('2013-02')>strtotime('1 Months')){
echo "true";
} else {
echo "false"; // got FALSE ... incorrect !
}

我在检查较新的日期时得到了错误的结果。

最佳答案

您忘记将 + 添加到您的 strtotime 函数。

if(strtotime('2013-02')>strtotime('+1 Months')){
echo "true";
} else {
echo "false";
}

更新:您的问题中有些事情很奇怪。例如 2013-02 不是日期而是对月份的引用。如果您想检查这是否是该月的第一天,请使用完整的日期表示法:2012-02-01。如果要检查当前日期是否为一个月,请使用 date("n") 检查当前月份(返回 1-12);并将其与给定月份进行比较,例如:

$date = "2012/02/01";

if(date("n", strtotime($date)) != date("n")) {
echo 'not current month';
}

如果您想检查这是否不是当前日期,请执行以下操作:

$date = "2012/02/01";

if(date('d-m-Y', strtotime($date)) != date('d-m-Y')) {
echo 'not current day';
}

关于php - 检查日期是否比当前日期更早或更新(按月),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14233883/

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