如果喜欢的话我需要制作 --->
如果您的活动少于 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") . ",";
}
我想我真的不懂日期计算。
在这种情况下
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
}
如果你想用 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";
}
我从你的代码中更改了几件事:
然后应用 json_encode 注释。
解释一下为什么逻辑颠倒了:
使用 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
$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) {
// ...
}