0

So, i'm working on grabbing a date of payment from a database, then add a year onto it as its expiry date. I want the user to know whether they are active or inactive when they log in.

However, everytime I login with someone who's payment hasn't expired yet, i get the you are an inactive member message. Can someone have a look and see where I am going wrong?

$paymentdate = strtotime($row[1]);
$paymentexpire = strtotime("+1 year", $paymentdate);
$currentdate = date('Y-m-d');
if($paymentdate != null && $paymentdate <= $currentdate && $currentdate < $paymentexpire){
    $msg = $p->addContent("You are an active member<br>");
}
else{
    $msg = $p->addContent("You are an inactive member<br>");
}
4
  • 2
    This part does not exactly make sense $paymentdate >= $paymentdate, the greater than part I mean... And in the second line $paymentdate is already a date which you are needlessly re-casting. Commented Sep 9, 2013 at 22:57
  • try breaking the conditions into three parts and testing each one with sample data. Can you supply some sample input? the problem could be there. Commented Sep 9, 2013 at 22:59
  • Why are you using strtotime($paymentdate) in $paymentexpire? You've already done that when setting $paymentdate! Commented Sep 9, 2013 at 23:02
  • 1
    strtotime() returns an integer timestamp, date('Y-m-d') returns a string, and you cannot reliably compare the two. You should be using time() as in @Sam's example below as it returns an integer timestamp. Commented Sep 9, 2013 at 23:10

1 Answer 1

1

See http://phpfiddle.org/main/code/ips-jgh for a PHP fiddle example.

This is a simplified version, and makes more sense. If the expiry date of the membership is greater/equal to the current time then the membership has not run out yet - active.

$paymentdate = strtotime($row[1]);
$paymentexpire = strtotime("+1 year", $paymentdate);

if($paymentdate != null && $paymentexpire >= time()){
    $msg = $p->addContent("You are an active member<br>");
}
else {
    $msg = $p->addContent("You are an inactive member<br>");
}

You could simplify the date conversion to:

$paymentexpire = strtotime("+1 year", strtotime($row[1]));

You modified your answer to include the current time, in string format. The code above uses only unix timestamps which are easy to understand and compare.

I've modified your example code and it works fine on my machine.

<?php

$date = "2012-10-10"; // member is active

$paymentdate = strtotime($date);
$paymentexpire = strtotime("+1 year", $paymentdate);

if($paymentdate != null && $paymentexpire >= time()){
   echo "You are an active member<br>";
}
else {
   echo "You are an inactive member<br>";
}

?>
Sign up to request clarification or add additional context in comments.

3 Comments

comes up like 2013-09-10 so i believe that is Y-m-d.
And again, the code is sound not more I can do. How about you print_r() / var_dump() the output of the database row?
okay, so when i put "2012-10-10" inside $paymentdate it works. That means i have other problems when reading the date from the database. Thanks for the help Sam :). Much appreciated.

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.