Jump to content
#FlattenTheCurve ×
ramiwahdan

check db date with current date

Recommended Posts

Hi,

I have database stored a field of date. I want to check if the date in the DB field is the same as the current date, how to do it?

Code:

$oracle = mysqli_real_escape_string($con, $_POST['oracle']);
$query 	= mysqli_query($con, "SELECT indate FROM staff WHERE OracleID ='$oracle'");
$row	= mysqli_fetch_array($query);
$num_row = mysqli_num_rows($query);
		
	if ($num_row > 0) 
	{	
		$curdate = date();
		if ($row['indate'] !== $curdate)
		{
			$query1=mysqli_query($con, "update staff set ClockedIn = 0, ClockedOut = 0 where OracleID='$session_id'")or die('Error In Session');
			header('location:home.php');				
		}
	}

 

db date.png

Share this post


Link to post
Share on other sites

You just have to make sure the date formats match. MySQL only uses yyyy-mm-dd, so:

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

 

  • Like 1

Share this post


Link to post
Share on other sites

Why are you doing a select query then checking the date and updating if not today.

All you need is single update similar to this

UPDATE staff 
    SET clockedIn = 0, 
        clockedOut = 0 
WHERE indate <> CURDATE()

 

Share this post


Link to post
Share on other sites
Posted (edited)

You should really check the num-rows value before you attempt to do a fetch.  Doesn't that make more sense?  Perhaps you should also check that the query call itself succeeded before doing that also.  Something like:

	if (query succeeded)
	     if (num_rows test)
	    {
	         (fetch)
	        (process results)
	    }
	

Edited by ginerjm

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.