Page 2 of 2

Re: query records by date range using php variables

Posted: Tue Apr 01, 2014 9:13 pm
by kwkened
Here's the code where I set the variables. At first I was trying them as strings which weren't working and thought I needed to get them as datetime type so I converted them as you'll see. Also not working -- obviously.

Code: Select all


$startdatestr = $_POST["StartDate"];
$enddatestr = $_POST["EndDate"];

$startdate = date_create("$startdatestr");
var_dump($stardate);

$enddate = date_create("$enddatestr");
var_dump($enddate);

Thank you.

Re: query records by date range using php variables

Posted: Wed Apr 02, 2014 12:37 am
by Christopher
You missed Celauran's comment. The create_date() function returns a DateTime object.

Code: Select all

$startdatestr = $_POST["StartDate"];
$enddatestr = $_POST["EndDate"];

$startdate = date_create($startdatestr);
var_dump($startdate->format('Y-m-d H:i:s'));

$enddate = date_create($enddatestr);
var_dump($enddate->format('Y-m-d'));
Obviously you need to add some checks to validate your post variables. And, I provided formats for DATETIME and DATE database fields.