LostSole Posted March 20, 2008 Share Posted March 20, 2008 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! Quote Link to comment Share on other sites More sharing options...
Perad Posted March 20, 2008 Share Posted March 20, 2008 I believe you need to use the strtotime function. This will create timestamp which can then be used to compare the dates with. http://uk3.php.net/strtotime Quote Link to comment Share on other sites More sharing options...
RMcLeod Posted March 20, 2008 Share Posted March 20, 2008 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'; } } Quote Link to comment Share on other sites More sharing options...
LostSole Posted March 20, 2008 Author Share Posted March 20, 2008 thank-you very much guys, you have both helped greatly! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.