query records by date range using php variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

kwkened
Forum Newbie
Posts: 8
Joined: Sun Mar 23, 2014 10:53 pm

Re: query records by date range using php variables

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: query records by date range using php variables

Post 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.
(#10850)
Post Reply