KommerSnart KommerSnart -5 years ago 118
SQL Question

Comparing date time todays date against date created

I'm stuck, been reading several places regarding comparing date and date time, but still can't figure out how to do it, anyone that could be so kind to help me out?

Thing is that I'm building a membership database and when the user register an account, it inserts date time to SQL, and that membership only last 1 year.
So the user has access to the page 1 year from the user registered.

Found this when reading, but not sure how I can check if the user still has access from the user registered. (365 days)

$today = date("Y-m-d H:i:s");
$expire = $isCreated; //from db

$today_time = strtotime($today);
$isCreated_time = strtotime($isCreated);

if ($isCreated_time < $today_time) {
/* has access*/
} else {
/* no access */

Thank's for all help or point in right way.

Answer Source

What you need is to find the difference between the Creation Date and today's Date and make sure that the difference is not more than 365 days to give the membership access.

This is the function to find the days between two dates in PHP:

 $now = time();
 $your_date = strtotime($isCreated);
 $datediff = $now - $your_date;
 echo floor($datediff / (60*60*24) );

So making the above inside a function will be like this:

function howLong($created) {
  $now = time(); // or your date as well
  $your_date = strtotime($created);
  $datediff = $now - $your_date;
  return floor($datediff / (60*60*24) );

Now you can check:

if (howLong($isCreated) > 365)
  echo "Membership Over";
  echo "Membership Active";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download