0

i'm saving time for first login ,now when user logs in i enter time using NOW() function, that saves time in this format (data type is DATETIME.

2015-12-24 15:47:30

Now logic is like every login is first login so i've to check if there already exists an entry for today to check that i fetch time explode it and get time like this

$logintime= mysqli_query($connection,"SELECT loggedin  from employees");              
$loggedin_time= mysqli_fetch_assoc($logintime);
$Date = $loggedin_time['loggedin'];
$loggedin_time_converted= explode(" ",$yourDate) ;
$ConvertedDate = $loggedin_time_converted[0]; 

last line returns 2015-12-24 now i've date

$today= time();
$DateToday= date("Y-m-d",$today);

$DateToday also returns me same format and same date now i need your help me to compare these dates , if they are equel i dont uopdate database if they are not i will , Pleas help me how do i compare these values

Sikander
  • 2,799
  • 12
  • 48
  • 100
  • Possible duplicate of [Compare given date with today](http://stackoverflow.com/questions/2113940/compare-given-date-with-today) – Ramesh-X Dec 24 '15 at 18:48

3 Answers3

4

You can do the test in MySQL

$result = mysqli_query($connection, "SELECT DATE(loggedin) = CURDATE() AS logged_in_today FROM employees");
$row = mysqli_fetch_assoc($result);
if (!$row['logged_in_today']) {
    // code to update database
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Wow, you've done all the hard stuff to get the problem to the point of being a simple comparison of 2 strings. This is all you need to do to get over the finish line ...

if ($ConvertedDate !== $DateToday) {
    // update the database
}
BareNakedCoder
  • 3,257
  • 2
  • 13
  • 16
0

You can use Php Built In function "Date Difference." Code Seems Like As Follow:-

$today= time();
$DateToday= date("Y-m-d",$today);
$diff = date_diff($today,$DateToday);
echo "$diff days";

This will return values something like +12 days or something else.

Mohammad Hani
  • 459
  • 5
  • 14