<?php
//These came from my database
$_SESSION['dateOfMembershipEnds'] = $row['dateOfMembershipEnds'];
if(empty($email)){
echo '<script>alert("Email is required!")</script>';
}else if(empty($password)){
echo '<script>alert("Password is required!")</script>';
}else if(strtotime($_SESSION['dateOfMembershipEnds']) >= date("Y-m-d")){
echo '<script>alert("Your Subscription Mr/Ms: '.$_SESSION['username'].' is already expired") </script>';
?>
I am expecting in my code that if the membership date is already expired the statement in my else if statement should be run.
CodePudding user response:
You're comparing strtotime
with date()
which will not give you results. Try this code and see if it works:
<?php
//These came from my database
$_SESSION['dateOfMembershipEnds'] = $row['dateOfMembershipEnds'];
if(empty($email)){
echo '<script>alert("Email is required!")</script>';
}else if(empty($password)){
echo '<script>alert("Password is required!")</script>';
}else if(strtotime($_SESSION['dateOfMembershipEnds']) <= strtotime("now")){
echo '<script>alert("Your Subscription Mr/Ms: '.$_SESSION['username'].' is already expired") </script>';
?>
Also not relevant to your question, It seems like it may be more logical for all of these statements to be just if
statements and not use else if
.
CodePudding user response:
You can do it dateformat like this before compare
else if(date('Y-m-d', strtotime($_SESSION['dateOfMembershipEnds'])) >= date("Y-m-d"))
The code will look like this
<?php
//These came from my database
$_SESSION['dateOfMembershipEnds'] = $row['dateOfMembershipEnds'];
if(empty($email)){
echo '<script>alert("Email is required!")</script>';
}else if(empty($password)){
echo '<script>alert("Password is required!")</script>';
}else if(date('Y-m-d', strtotime($_SESSION['dateOfMembershipEnds'])) >= date("Y-m-d")){
echo '<script>alert("Your Subscription Mr/Ms: '.$_SESSION['username'].' is already expired") </script>';
}
?>