PHP if date is older than X days

Viewed 9342

I have some problems with dates. I need make if like --->

if your activity is less than 1 day do somethink

else if your activity is more than 1 day and less than 3 do moething else

else if your activity is more than 3 do moething else

I need this in PHP. My actual code is:

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") . ",";
}

I think I really don't understand date calculations.

3 Answers

Date_diff is much easier in this case:

$datetime1 = date_create(); // now
$datetime2 = date_create($last_log);

$interval = date_diff($datetime1, $datetime2);

$days = $interval->format('%d'); // the time between your last login and now in days

see: http://php.net/manual/en/function.date-diff.php

Or in your way:

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

If you want to do it with strtotime, do it like this:

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";
}

Couple things I changed from your code:

  • remove the strtotime(strtotime()). Do not do it twice!
  • For your second if, I added parentheses to ensure correct evaluation of conditions.
  • I reversed the order of your if. First check if it is very old (so < -3). Then check if it is between -3 and -1. Then check between -1 and now.
  • Added <= and >=. The = cases were missing from your code. So if the last_log was == -1, it was not processed ever.
  • I replace "else if" by "elseif".
  • I used variables because recalculating strtotime all over is wasteful. And it makes the code less readable IMHO.

Then apply the json_encode comment.

To explain why the logic was reversed:

  • the last login of a user will always be before now.
  • lets say that the user's last_login is 5 days ago. strtotime($last_login) will be smaller than strtotime('-1 days'), so the if will be true. But that is not what the OP wants! He wants here the case where the last login is older than 3 days.
  • Remember that we are comparing numbers in the past, so the smaller, the older.
$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) {
        // ...
}

The doc is here : http://php.net/manual/en/datetime.diff.php

Related