Hi all - also new to PHP/SQL and have a different question on a similar question posted -
I need to create a page in php that allows the user to select a date range to and from.
Howver, the date stored in the database is unix such as 1494548767
Was thinking a drop down calender might work too - just not sure where to go with this or how to search by date in this situation.
If you need table structure or anything else - please reply and I'll provide what is needed to help find a solution.
Thanks in advance!
J

Recommended Answers

All 6 Replies

Yep, Been there done that thus how/why I landed here.
The key to my question was to see and use regular dates on the search page although the search range is a unix based date code NOT MM-DD-YYY. Maybe I didn't explain that clearly, my apologies.

You need to convert the dates users enter to Unix timestamps. You can do that either in PHP or MySQL (see UNIX_TIMESTAMP()).

The exact syntax depends on how you're interacting with your database (MySQLi, PDO, etc.). You basically need to take the POSTed form values, do some straightforward validation to make sure that the user hasn't entered anything dangerous, then insert the values into the query:

$from = mysqli_real_escape_string($_POST['fromdate']);
$to = mysqli_real_escape_string($_POST['todate']);
$sql = "select id, name, brand, datecreated from product where datecreated between '$from' and 
'$to' order by datecreated desc";

The action on the form would be whichever page you place the above code in - it could be the same page as the form. In that case, the action would be

<?php echo basename($_SERVER['PHP_SELF']); ?>

and the method would be POST.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.