Jump to content

How to select records by a given date range ?


adrianTNT

Recommended Posts

Hello.
In my database I have records (transactions) that have a transaction_date field.
The transaction_date filed value is formatted like this: "2006-09-15 00:00:00"
I will send the month, day and year as URL variable (I will send both start date and end date)
[b]How should the PHP code look in order to show only the records in that date range?[/b] ???
Thank you.

- Adrian.
Link to comment
Share on other sites

you'd have to set up your start and end variables in a timestamp format first (YYYY-MM-DD), then, you simply run a query to see what is in between them:
[code]
<?php
$start = "$stYear-$stMonth-$stDay";
$end  = "$edYear-$edMonth-$edDay";
$sql = mysql_query("SELECT * FROM tableName WHERE DATE(dateCol) >= $start AND DATE(dateCol) <= $end");
?>
[/code]

hope this helps
Link to comment
Share on other sites

I made some more experiments while waiting a reply, I got to something that worked by this code:

[code]<?php
$start_date = ($start_year.'-'.$start_month.'-'.$start_day.' '.$start_hour.':'.$start_minutes.':'.$start_seconds);
$end_date = ($end_year.'-'.$end_month.'-'.$end_day.' '.$end_hour.':'.$end_minutes.':'.$end_seconds);
// add some slashes needed in query format
$start_date="'".$start_date."'";
$end_date="'".$end_date."'"; ?>
[/code]
Then lower in code I have:
[code]<?php
$query_Recordset_transactions = sprintf("SELECT * FROM transactions WHERE tr_user_id = %s AND tr_date BETWEEN $start_date and $end_date", GetSQLValueString($colname_Recordset_transactions, "text")); ?>
[/code]
This works for now so I will use this one if it will not generate problems.
Thanks for both your replies.
Link to comment
Share on other sites

I got to another issue, I hope one of you can help :)
I needed to display records between two given dates, it works fine.
Then I added this code...
if start and end date ranges are not set then end_date is today and start_date is 7 days ago; so that by default it displays records for last 7 days.
[b]Is there a function to decrease 7 days from a date like '2006-09-06 00:00:00' ?[/b] A code block is also nice if there isn't a function for this :)

Thanks.
- Adrian.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.