Can we use PHP function strtotime in Mysql Query

mysql, php, strtotime

Solution

Your code is `NOT` a valid PHP syntax. Code below is a clean and proper way of doing this:

$from = date('Y-m-d', strtotime($_POST["DateFrom1"]));
$to = date('Y-m-d', strtotime($_POST["DateTo1"]));
$query = "select * from table where date between '$from' and '$to'";
// use this query to your mysqli

Problem

I have the following MySQL syntax which gives me an error. I am aware that you cannot compare date variables directly, so I use `strtotime` to create a Unix timestamp to compare dates. Can you use the PHP `strtotime` function within a MySQL query? ``` $result = select * from table where strtotime(Date) >= strtotime($_POST[DateFrom1]) && strtotime(Date) <= strtotime($_POST[DateTo1])"; ```

Original source