Skip to main content
airsoftie
Participant
April 26, 2011
Question

PHP/MySQL date range search

  • April 26, 2011
  • 1 reply
  • 2185 views

Hi, all...

I'm building a joblog.

I want to search a range of dates (...WHERE timestamp >= $date1 AND timestamp <= $date2...).

I built my own query, and it worked using strtotime, etc..

$searchFrom = strtotime($_POST['searchFrom']);

$searchFrom = date("Y-m-d 00:00:00",$searchFrom);

$searchTo = strtotime($_POST['searchTo']);

$searchTo = date("Y-m-d 24:59:59",$searchTo);

However, I wanted to use DW's Master Detail Page.

(Trying to save time, not going so well...)

When I setup my query and all vars, It wigs out on the dates everytime.

Is there something in the recordset that is stripping away the formatting I put into my vars?

This topic has been closed for replies.

1 reply

Inspiring
April 26, 2011

Is the database field a date field or datetime.  Also is the user entering the date in the field like YYYY-MM-DD format?  Normally I require my users to enter the date in YYYY-MM-DD format and then on my search page I do this after creating sessions so I can use the dates in other pages:

<?php
$startdate=$_SESSION['start_date'];
$startdate=$startdate . " 00:00:00";
$enddate=$_SESSION['end_date'];
$enddate=$enddate . " 23:59:59";?>

Then my query would be timstamp between '$startdate' and '$enddate'  Make sure to have the single quotes around the date range.

airsoftie
airsoftieAuthor
Participant
April 26, 2011

Thanks. I like your method. I found out that it was the single quotes that got me.

Now, if only I could get my pagers to work. You wouldn't know how variables are passed to keep pagination correct, would you?

Inspiring
April 26, 2011

Not sure what you mean by keeping pagination correct?  However, to pass variables to all my pages I setup sessions.  So in my example of the date range.  I setup my form to go to a page that has this code:

<?php

session_start();

$_SESSION['start_date']=$_POST['start_date'];

$_SESSION['end_date']=$_POST['end_date'];

header ('Location: search_date.php');

?>

With this code I set up two sessions for start date and end date and then forward the user on.  Each page after that must start with <?php session_start();?>

Now that the session is stored I can use those variables on all of my pages.  This is very usefull when you want take the person to a page and let them view brief details and then forward on to more details and the ability to come back to your original search page.

Also usefull if you are updating a record and then on the next page want to setup an email script.