确定给定日期是否小于 1 天、1 到 3 天或更长时间

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

如果喜欢的话我需要制作 --->

如果您的活动少于 1 天,请做某事

如果您的活动超过 1 天且少于 3 天,请做其他事情

否则,如果您的活动超过 3 次,请做其他事情

我需要这个 PHP 版本。我的实际代码是:

if (strtotime(strtotime($last_log)) < strtotime('-1 day') ) {
    $prom .= "" . json_encode('last_activity') . ": " . json_encode("inactive less than 1 day") . ",";
} else if (strtotime($last_log) > strtotime('-1 day') && strtotime($last_log) < strtotime('-3 day')) {
    $prom .= "" . json_encode('last_activity') . ": " . json_encode("inactive more than 1 day and less than 3 days") . ",";
} else if (strtotime($last_log) > strtotime('-3 day')) {
    $prom .= "" . json_encode('last_activity') . ": " . json_encode("inactive more than 3") . ",";
}

我想我真的不懂日期计算。

php datetime if-statement date-comparison
4个回答
8
投票

在这种情况下

date_diff
要容易得多(http://php.net/manual/en/function.date-diff.php):

$datetime1 = date_create(); // now
$datetime2 = date_create($last_log);
    
$interval = date_diff($datetime1, $datetime2);

// number of days between your last login and now
$days = $interval->format('%a');

// difference (only) in days
// (2023-11-05 and 2024-01-08 will return 3 because 05 and 08 are seperated by 3 days.
$days = $interval->format('%d');

更多格式请参阅:https://www.php.net/manual/en/dateinterval.format.php

或者以你的方式:

if (strtotime($last_log) < strtotime('-1 day')){
    // it's been longer than one day
}

1
投票

如果你想用 strtotime 来做到这一点,请这样做:

date_default_timezone_set('SOMETHING FOR YOU');

$last_log = '-0.5 day';

$last_log_time = strtotime($last_log);
$minus1day_time = strtotime('-1 day');
$minus3day_time = strtotime('-3 day');

echo $last_log_time . "<br>";
echo $minus1day_time . "<br>";
echo $minus3day_time . "<br>";

if ($last_log_time < $minus3day_time)
{
    echo "inactive more than 3";
}
elseif ( ($last_log_time <= $minus1day_time) && ($last_log_time >= $minus3day_time) )
{
    echo "inactive more than 1 day and less than 3 days";
}
elseif ($last_log_time > $minus1day_time)
{
    echo "inactive less than 1";
}

我从你的代码中更改了几件事:

  • 删除 strtotime(strtotime())。不要重复两次!
  • 对于第二个 if,我添加了括号以确保正确评估条件。
  • 我颠倒了你的if的顺序。首先检查它是否很旧(所以< -3). Then check if it is between -3 and -1. Then check between -1 and now.
  • 添加<= and >=。您的代码中缺少= 案例。因此,如果 last_log == -1,则它永远不会被处理。
  • 我将“else if”替换为“elseif”。
  • 我使用变量是因为重新计算 strtotime 很浪费。恕我直言,它使代码的可读性降低。

然后应用 json_encode 注释。

解释一下为什么逻辑颠倒了:

  • 用户的最后一次登录总是在现在之前。
  • 假设用户的last_login 是 5 天前。 strtotime($last_login) 将小于 strtotime('-1 days'),因此 if 将为 true。但这不是OP想要的!他想要的是最后一次登录超过 3 天的情况。
  • 请记住,我们正在比较过去的数字,因此越小,越旧。

0
投票

使用 DateTime 对象和

match()
的一些现代建议。

对于您的场景:

$UTC = new DateTime('midnight', new DateTimeZone('UTC'));
$daysPast = (int) $UTC->diff(new DateTime($last_log))->format('%r%a');
echo match (true) {
    $daysPast >   -1 => 'active',
    $daysPast === -1 => 'inactive less than 1 day',
    $daysPast >   -4 => 'inactive more than 1 day and less than 3 days',
    default          => 'inactive more than 3'
};

作为包含一系列动态测试日期的更完整演示:(Demo)

$testPeriod = new DatePeriod(
    new DateTime('midnight -5 days', new DateTimeZone('UTC')),
    new DateInterval('P1D'),
    new DateTime('midnight +2 days', new DateTimeZone('UTC')),
    DatePeriod::INCLUDE_END_DATE
);

$UTC = new DateTime('midnight', new DateTimeZone('UTC'));
foreach ($testPeriod as $last_log) {
    // if $last_log is just a string, use: new DateTime($last_log)
    $daysPast = (int) $UTC->diff($last_log)->format('%r%a');
    printf(
        "\nComparing: %s versus %s (%d days past) : ",
        $UTC->format('Y-m-d'),
        $last_log->format('Y-m-d'),
        $daysPast
    );
    echo match (true) {
        $daysPast >   -1 => 'active',
        $daysPast === -1 => 'inactive less than 1 day',
        $daysPast >   -4 => 'inactive more than 1 day and less than 3 days',
        default          => 'inactive more than 3'
    };
}

输出:

Comparing: 2024-01-30 versus 2024-01-25 (-5 days past) : inactive more than 3
Comparing: 2024-01-30 versus 2024-01-26 (-4 days past) : inactive more than 3
Comparing: 2024-01-30 versus 2024-01-27 (-3 days past) : inactive more than 1 day and less than 3 days
Comparing: 2024-01-30 versus 2024-01-28 (-2 days past) : inactive more than 1 day and less than 3 days
Comparing: 2024-01-30 versus 2024-01-29 (-1 days past) : inactive less than 1 day
Comparing: 2024-01-30 versus 2024-01-30 (0 days past) : active
Comparing: 2024-01-30 versus 2024-01-31 (1 days past) : active
Comparing: 2024-01-30 versus 2024-02-01 (2 days past) : active

-1
投票
$dateLog = new DateTime($last_log); // format if needed

$tomorrow     = new DateTime("tomorrow");
$yesterday    = new DateTime("yesterday");
$threeDaysAgo = new DateTime("-3 days");

if ($dateLog < $yesterday) {
        // Do what you want
} else if ($dateLog > $yesterday && $dateLog < $threeDaysAgo) {
        // Do another thing
} else if ($dateLog > $threeDaysAgo) {
        // ...
}

文档在这里:http://php.net/manual/en/datetime.diff.php

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