Jump to content

Comparing Dates


LostSole

Recommended Posts

Hi Guys!

 

Im having a little trouble comparing two dates as I've never worked with dates before. One is the current date and the other is stored in a database with field-type timestamp. I was hoping someone could teach me how to:

Compare the date in the database with todays date and if the date is less than 1 month away, tell the user they need to update their subscription, otherwise show the number of days left for their subscription.

 

//Get the date from the Database in the format DD-MM-YYYY
$queryExpiresOn = "SELECT DATE_FORMAT(AcctExpiresOn, '%d-%m-%Y') AS new_date FROM account WHERE AcctID = ".$acctID."";
$resultExpiresOn = mysql_query($queryExpiresOn) or die(mysql_error());

$expiryDate = date(mysql_result($resultExpiresOn,0,"new_date"));
$todaysDate = date("d-m-Y");

if ($expiryDate < $todaysDate) {                      // here I dont know how to add/subtract 11 months to $expiryDate
echo "Your subscription still has ___ days left";     // here i dont know how to work out how many days are left
}

else {
echo "Please increase your subscription";     
}

 

Thanks alot for your help!

 

 

 

Link to comment
Share on other sites

I think the easiest way to do this is to get the timestamp out of the database as is, this makes doing maths on it easier.

 

Assuming you have a variable called $expiry which contains the timestamp pulled from the database try this.

 

$today = strtotime(date('d-m-Y')); //Gets the timestamp for today

 

if($expiry < $today) { // Check if expiry date is in the past

  echo 'Your subscription has expired';

} else {

  $daysleft = ($expiry - $today) / 86400; //timestamps are in seconds, there are 86400 seconds in a day

  if($daysleft < 31) {

    echo 'Please update your subscription';

  } else {

    echo 'You have ' . $daysleft . ' days left for your subscription';

  }

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.