0

I want to get data from Mysql With Php between 2 dates. I am unable to get this.. Here are my Codes. These Lines are for Date Interval...

date_default_timezone_set("EST");
$today = date('d-m-Y');
$date_to = $today;
$date_to = strtotime("-7 days", strtotime($date_to)); //-7 days for last week. -30 for last week
$date_to = date("d-m-Y", $date_to);

Now Here are the Queries Which i am running.. I have two conditions. 1 The Data should be between $today and $date_to dates. And assigned should be equal to 1. Assigned is a column in Database table 'requests'.

$get_req = "SELECT * FROM `requests` WHERE `req_date` between '$today' AND '$date_to' AND `assigned`='1'";
$result = mysqli_query($dbc, $get_req);
while ($res = mysqli_fetch_assoc($result)){
    $driver_id = $res['assigned_driver_id'];
    $req_id = $res['req_id'];
    $req_title = $res['request_title'];
    $req_price = $res['price'];
    $req_time = $res['request_time'];
    $req_date = $res['req_date'];
    $driver = $res['driver_name'];
}

Here i am unable to get my required result. Problem is Only in Date query. I mean in Between Date A to Date B. req_date` between '$today' AND '$date_to' It works Perfect.

Please advise me what i should do to fix it. I want to get the Records of Past 7 Days only.

8
  • Is req_date in d-m-Y format? I'd think it would be a date or datetime column in YYYY-MM-DD (mysql date format, not PHP date codes). Commented Jul 11, 2015 at 18:06
  • No i have used same format for Database. (DD-MM-YYYY) Commented Jul 11, 2015 at 18:10
  • What is the column type? dev.mysql.com/doc/refman/5.6/en/datetime.html If it is date/datetime you could use some predefined functions to calculate out week, month etc. stackoverflow.com/questions/7391718/… Commented Jul 11, 2015 at 18:13
  • Column type is varchar Commented Jul 11, 2015 at 18:21
  • So that is why between isn't working. As it currently stands mysql just thinks it is text. It should be a date or datetime column (you will need to update existing data). Maybe this would be useful, stackoverflow.com/questions/15396058/… Commented Jul 11, 2015 at 18:24

2 Answers 2

2

I don't know what's your DB structure, but you could change your code and do something like:

Your PHP Code

date_default_timezone_set("EST");
$today = time();
$date_to = strtotime("-7 days"); //-7 days for last week. -30 for last week

And your query:

$get_req = "SELECT * FROM `requests` WHERE UNIX_TIMESTAMP(`req_date`) between '$today' AND '$date_to' AND `assigned`='1'";
Sign up to request clarification or add additional context in comments.

2 Comments

$date_to is displayed in what structure? ex: yyyy-MM-dd hh:mm:ss ?
strtotime returns a unix timestamp. Doc here: php.net/manual/en/function.strtotime.php
0

Here is your solution. You need to change the query like this.

STR_TO_DATE(yourdatefield, '%d-%m-%Y') > CURDATE() - INTERVAL 7 DAYS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.