比较 PHP 中字符串的日期

问题描述 投票:0回答:1

在我的 php 中有 2 个表示日期的字符串,格式为“dd/mm/yyyy”。我想将第二个和第一个 + 3 年进行比较。我尝试了这个,但尽管更改了日期,但总是给我返回相同的响应。

private function setBonus($s1, $s2) : string{
        $new_s1 = strtotime(str_replace('/', '-', $s1));
        $new_s2 = strtotime(str_replace('/', '-', $s2));
        $targetBonus = strtotime('+3 years', $s1);
        $targetBonus = strtotime('last day of May', $targetBonus);

        
        return (targetBonus > new_s2)? "YES" : "NO";
    }
php string date strtotime
1个回答
0
投票

更正:

private function setBonus($s1, $s2): string{
    $new_s1 = strtotime(str_replace('/', '-', $s1));
    $new_s2 = strtotime(str_replace('/', '-', $s2));
    $targetBonus = strtotime('+3 years', $new_s1);
    $targetBonus = strtotime('last day of May', $targetBonus);

    return ($targetBonus > $new_s2) ? "YES" : "NO";
}

解释:
计算目标奖金日期时使用 $new_s1 而不是 $s1。设置目标奖金日期时将变量名称更正为 $targetBonus。在 return 语句中使用了正确的变量名称。

© www.soinside.com 2019 - 2024. All rights reserved.