2

In my database I have one table in which I keep registered users. One column is Date of register and I keep this value in my own string format. For example "[2013-11-30] [19:42:46]"

Then I want to make a check. If user is 30 days old or more. The sure thing is that the above code is wrong. The problem is that if one user registers at 29/01/2015 will not been showing in 30 last days if the current day is 02/02/2015!

//Datetime
   $today = date_parse_from_format("[Y-m-d] [H:i:s]", gmdate("[Y-m-d] [H:i:s]"));
   $store = date_parse_from_format("[Y-m-d] [H:i:s]", $row["LastSeen"]);

   if (
        (($store[year] >= $today[year]) && ($store[month] >= $today[month]))
      )
   { $date_last = "<font color='green'>".$row["LastSeen"]."</font>"; }
   else
   { $date_last = "<font color='red'>".$row["LastSeen"]."</font>"; }

1 Answer 1

1

Use date_create_from_format instead of date_parse_from_format. Then you can simply compare the resulting values:

$today = date_create_from_format("[Y-m-d] [H:i:s]", gmdate("[Y-m-d] [H:i:s]"));
$store = date_create_from_format("[Y-m-d] [H:i:s]", $row["LastSeen"]);

if ($store < $today) {
    // ...
}
else {
    // ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but i also tried this and date_create_from_format doesn't work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.