0

I want to compare two datetime variable witch one of them is current server time and one of them is stored in mysql database i am running this simple piece of code and it wont work and says it is a syntax error i am wondering what can i do

    <?php include("includes/student_session.php");?>
<?php confirm_logged_in();?>
<?php include("includes/connection.php");?>
<?php 
        date_default_timezone_set("");
        $now_date=date("Y-m-d H:i:s");

    $query1="select exam_id from exam where {$now_date} > start_date";
    $result=mysqli_query($cnn,$query1);
    if($result){

    $row=mysqli_fetch_array($result);
    echo $exam_id. "exists";

    }else
    {echo mysqli_error($cnn);}

?>
2
  • on which line you are getting syntax error ? Commented Sep 17, 2014 at 7:56
  • You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'query1' at line 1 Commented Sep 17, 2014 at 7:58

3 Answers 3

3

In this code:

$query1="select exam_id from exam where {$now_date} > start_date";
$result=mysqli_query($cnn,@query1); // <-

You are using @query1, this should be $query1.

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

3 Comments

yup that's it, additionally you have to be sure to have the mysql server using the same timezone as your apache/php one. The best is to configure in mysqld.ini and in php.ini the timezone so that it is using UTC
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '12:30:21' at line 1 @phantom
You should be using timestamps to compare date/time values. As stated in the answer by Raheel, you should be using start_date > '{$now_date}' (or <, no quotes req. if you're using timestamps) instead of {$now_date} > start_date.
1
$query1="select exam_id from exam where {$now_date} > start_date";
$result=mysqli_query($cnn,@query1);

I think the error is because you are using @ insteal of $. Also your query seems to be wrong. You should compare values with fields change it to

   $query1="select exam_id from exam where start_date > '{$now_date}'";
   $result=mysqli_query($cnn,$query1);

Comments

1

Even if correct the misstype of @query1 the query will not work like this. $now_date is a string and you have to pass it to the query like a string. Even better if you format it with mysql's functions, not with php's one as well as compare time stored in database with database's own time (then you can skip any timezone problems). So, your query, for example, could be like this:

SELECT exam_id FROM exam WHERE DATEDIFF(NOW(), DATE_FORMAT(start_date,'%Y-%m-%d %T')) > 0;

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.