0

I have an Android app which is used to store data of users in MySQL database using PHP. I need to do some validations according to the date.The MySQL Database has a predefined date for each user.The date increases by 1 day whenever a user inserts data twice for that day.And when the date exceeds the current date,it shows a message that "the user has already submitted data for the day".

My PHP file is :

  <?php
    require "conn.php";
    $user_mobile =  $_POST["mobile_num"];
    $user_pass = $_POST["password"];


    $mysql_qry = "select * from employee_data where mobile like '$user_mobile' and password like '$user_pass';";
    $result = mysqli_query($conn,$mysql_qry);
    if(mysqli_num_rows($result) > 0 ) {
         $row = mysqli_fetch_assoc($result);
         $name= $row["name"];
         $_POST['user'] = $name;
         $last_updated = $row["last_updated_date"];
         $_POST['user'] = $name;
         $_POST['date'] = $last_updated;
         echo "Login successful. Welcome_" .$name; 
         echo "_Date:" .$last_updated;  

    $now = new DateTime("Asia/Kolkata");
    $n = ($now->format('Y-m-d'));


    if(($last_updated > $n)) {
       echo "exceeded";
      }

?>

But what is happening is if "$last_updated"(the date which is changing everytime) is the current date,then it is not going inside the if condition. So if "$last_updated" is today's date, then the user gets the message "the user has already submitted data for the day". I tried doing an echo of "$n" and it gives the current date. So, it should not go to the if-condition because ($last_updated == $n) . But its going inside the if-condition when ($last_updated == $n).I don't know why this is happening.Can anyone please help me with this?

2
  • your code is vulnerable to sql injection. please fix. Commented Mar 17, 2017 at 6:42
  • what's the last_updated_date field data type ? Commented Mar 17, 2017 at 6:47

3 Answers 3

2

Try to change your

    $now = new DateTime("Asia/Kolkata");

to:

    $now = new DateTime(null, new DateTimeZone('Asia/Kolkata'));
Sign up to request clarification or add additional context in comments.

Comments

1

You need to compare them as objects.. Try something like this..

$now = new DateTime("Asia/Kolkata");

if (new DateTime($last_updated) > $now) {
    echo "exceeded";
}

I'm not sure if your timezones are set properly on your server but to be sure you could do one of the following as well..

$now = new DateTime('Asia/Kolkata');

if (new DateTime($last_updated, new DateTimeZone('Asia/Kolkata')) > $now) {
    echo "exceeded";
}

Or probably even better..

date_default_timezone_set('Asia/Kolkata');

$now = new DateTime('now');

if (new DateTime($last_updated) > $now) {
    echo "exceeded";
}

2 Comments

this does not work. Actually $last_updated is only date.Its in the format 2017-03-17.So this code is not working. Can you let me know how to check the conditon with only date? That is why I tried with ($now->format('Y-m-d')) to get only the date.
you need to remove this line $n = ($now->format('Y-m-d'));
0

Your issue is that since you are not keeping the time in the database and when you compare the date with now which includes the time, the now will always be greater than the date you pull from the database.

The database time 2017-03-17 becomes 2017-03-17 00:00:00 php time.

When you generate php now time it will be 2017-03-17 xx:xx:xx which will always be greater than database time

  $last_updated = new DateTime($row["last_updated_date"],new DateTimeZone("Asia/Kolkata"));

  $today = new DateTime('now',new DateTimeZone("Asia/Kolkata")); //gets current date time
  $today->setTime(0,0);  // back to the beginning of the day

  //echo $last_updated->getTimestamp .  | . $today->getTimestamp; // for debugging purpose 
  if(($last_updated->getTimestamp >= $today->getTimestamp)) { 
    // note that I have used '>=' to allow the current date 

  }

You can also use the DateTime->diff() function.

Reference: http://php.net/manual/en/class.datetime.php

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.