Anuj Hari
Anuj Hari

Reputation: 543

php dates and if statements

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>");
}

Upvotes: 0

Views: 69

Answers (1)

SamV
SamV

Reputation: 7586

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>";
}

?>

Upvotes: 1

Related Questions