Reputation: 27
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?
Upvotes: 0
Views: 3741
Reputation: 601
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
Upvotes: 0
Reputation: 125
Try to change your
$now = new DateTime("Asia/Kolkata");
to:
$now = new DateTime(null, new DateTimeZone('Asia/Kolkata'));
Upvotes: 2
Reputation: 711
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";
}
Upvotes: 1