0

ok basically I am trying to compare today date with a database date and determine if today date is > then one of the dates in database. but no matter what date is picked up from datatabase < or > compared to today the result is always true.

Could some one pleas have a look at my code and point out any potential wrongdoings please

PHP

$timestamp = date("Y-m-d");

$sql="SELECT enddate FROM campaigns WHERE id=".$data['camp'];
    $query = mysqli_query($db, $sql);

while($result = mysqli_fetch_assoc($query)){
    if($timestamp > $result){

        echo "<script type='text/javascript'>alert('This Workshop Has Expired');</script>";
        exit;
    }else{

        echo "<script type='text/javascript'>alert('Thank You For Registering');</script>";
        exit;
    }
}
2
  • In your database, how do you store "enddate"? You should be using timestamps for this really, and looking at how you make you $timestamp var I'm guessing you dont. Commented Dec 11, 2013 at 15:33
  • if($timestamp > $result){ ?? how does php know it is a "date" ? convert to integer using strtotime and then verify. in real life applications use datetime class. Commented Dec 11, 2013 at 15:34

3 Answers 3

3

Try to for example :

$var = "2010-01-21 00:00:00.0"//date from DB
strtotime($var);

It turns it into a time value, and:

time() - strtotime($var);

gives you the seconds since $var

To check if $var has been within the last day try:

if((time()-(60*60*24)) < strtotime($var))

That's all! :)

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

1 Comment

and yes i have tried var_dump both formats looked the same as well as i have tried strtotime the problem seemd to be in my if statment, but thank you for trying to help
1
if($timestamp > $result['enddate']){

$result is an array, as called by $result = mysqli_fetch_assoc($query). mysqli_fetch_assoc returns an array with the column name of your row as keys and the content of each field as value.

1 Comment

owww loool it works but when i did var_dump on each variable $timestamp and $results the numbers looked correct and corresponding both to database and today date, sorr if it is a lame question but could you explain to me why i needed to add $result['enddate']
1

The problem is that the "enddate" from your database isn't a number, it's a string.

You should convert it to numbers:

$now = time();
$sql="SELECT enddate FROM campaigns WHERE id=".$data['camp'];
$query = mysqli_query($db, $sql);
while($result = mysqli_fetch_assoc($query)){
    $db = strtotime($result);
    if($now > $db) {
        // expired
        echo "<script type='text/javascript'>alert('This Workshop Has Expired'); </script>";
        exit;
    }
    else {
        // not expired
        echo "<script type='text/javascript'>alert('Thank You For Registering');</script>";
        exit;
    }
}

Comments

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.