Filter results by date in timestamp field Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Filter results by date in timestamp field without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have already had some help but not sure why this isn’t working.

I am trying to use a form to let a user filter their activity (which is stored in a DB)

My code:

$_GET['from'] = '01/11/2013';
$_GET['to']   = '25/11/2013';

$from = DateTime::createFromFormat('d/m/Y', $_GET['from']);
$to   = DateTime::createFromFormat('d/m/Y', $_GET['to']);

$sql = "
    SELECT * FROM transfer 
    WHERE personID = $user AND DATE(time) BETWEEN '%s' AND '%s'
";
$sql = sprintf($sql, $from->format('Y-m-d'), $to->format('Y-m-d'));

print_r($sql);

This prints SELECT * FROM transfer WHERE personID = 84587749 AND DATE(time) BETWEEN '2013-11-01' AND '2013-11-14'

When I query this in PHPmyadmin it shows the record, however not showing in my page?

Answer

The SQL looks fine but you don’t appear to have issued the executed the SQL query in the database and retrieved the results?? Maybe I’m missing something but you need to connect to your database:

class DBi {
  public static $mysqli;
}

DBi::$mysqli = new mysqli('servername', 'database', 'password', 'user');

if (mysqli_connect_error()) {
  die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
}

Then you need to perform the query:

$result = DBi::$mysqli->query($sql) or die ("Unable to execute SQL command:".$sql);

And finally, retrieve and use the result:

$row = $result->fetch_assoc();
echo $row["fieldname"];
We are here to answer your question about Filter results by date in timestamp field - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji