Select data between two dates? - php

I'm using a database to store logs, with a column "date" which holds the date it was inserted. The format of the date is "MM/DD/YY". Please can anyone suggest how I would SELECT data in between two certain dates. For example, I tried this:
$from_date = "01/01/12";
$to_date = "02/11/12";
$result = mysql_query("SELECT * FROM logs WHERE date >= " . $from_date . " AND date <= " . $to_date . " ORDER by id DESC");
while($row = mysql_fetch_array($result)) {
// display results here
}
But I guess this doesn't work because the dates aren't numbers. Thanks for the help! :)

Use the BETWEEN keyword:
"SELECT * FROM logs WHERE date BETWEEN '" . $from_date . "' AND '" . $to_date . "'
ORDER by id DESC"

You can cast the fields as dates and then select between from_date and to_date
SELECT * FROM logs WHERE date STR_TO_DATE(date, '%m/%d/%Y') between STR_TO_DATE(from_date, '%m/%d/%Y') and STR_TO_DATE(to_date, '%m/%d/%Y')

The answer to your question depends on the data type that is used to store the date field in the logs table.
SQL (MySQL in your case) is fully capable of comparing dates. Usually, the BETWEEN .. AND .. operator is used but that will not work correctly if the type of date is CHAR (or VARCHAR) - in which case you will need to cast the date field to a DATETIME before comparing.

You need to add single quotes to the date values '01/01/12':
$from_date = "01/01/12";
$to_date = "02/11/12";
$result = mysql_query("SELECT * FROM logs WHERE date >= '" . $from_date . "' AND date <= '" . $to_date . "' ORDER by id DESC");

Change date parameters into Unix timestamps and then compare them. Here is the code:
$from_date = "2019/01/12";
$to_date = "2019/01/15";
$from_date_unix = strtotime($from_date);
$to_date_unix = strtotime($to_date);
$result = mysql_query("SELECT * FROM logs WHERE date >= " . $from_date_unix . " AND date <= " . $to_date_unix . " ORDER by id DESC");
while($row = mysql_fetch_array($result)) {
// display results here
}

Related

wpdb->prepare() returns NULL

I can't figure out why this is returning "NULL". I've hardcoded the date/time string in to the $scheduledDates variable. In practice that's user input. Everything works fine when I don't prepare the query.
$scheduledDate = "2015-09-01 00:00:00";
$queryString = "SELECT * FROM schedules WHERE event_start > %s AND event_start < %s + INTERVAL 1 DAY";
$scheduled_blocks = $wpdb->get_results( $wpdb->prepare( $queryString, $scheduledDate ) );
The code below works fine, whether I hard code the date/time or not...
$scheduledDate = $_POST['scheduledDate'];
$scheduled_blocks = $wpdb->get_results('SELECT * FROM schedules WHERE event_start > "' . $scheduledDate . '" AND event_start < "' . $scheduledDate . '" + INTERVAL 1 DAY');
Use $wpdb->print_error() to see what errors you get. By the look of your code though, I think the number of placeholders have to be the same as the amount of values you're supplying to the prepare method. Alter your call to this:
$wpdb->prepare($queryString, $scheduledDate, $scheduledDate)

PHP query between two dates?

I am trying to set a table up to display out of date tests. So, I have a test that is dated 28/01/2014 and the next test is due on 28/01/2015. As of today's date, this is out of date.
My query looks like the following:
$expiry = date("Y-m-d");
$queryout = "SELECT appliances.*, tests.* FROM (appliances LEFT JOIN tests
ON appliances.ID = tests.Appliance) WHERE Cli_ID = '$useractiveid'
AND `Next Test Due` BETWEEN `Date Tested` AND '" . $expiry . "'";
I want the query to find tests that are out of date where the Next Test Due is out of date.
Here is my answer based on my comments:
$expiry = date("Y-m-d");
$queryout = "SELECT appliances.*, tests.* FROM (appliances LEFT JOIN tests
ON appliances.ID = tests.Appliance) WHERE Cli_ID = '$useractiveid'
AND where Next Test Due < '" . $expiry . "'";

MySQL query with date parameter

My table in mysql has special data stamp (startdata). It is date when event starts. Older events stored in database to, but i don't need them to appear in the MySQL answer.
So is there any way sending query to database that includes parameter i need? (for example, not showing rows where startdate older than today).
Now my code looks like:
$res3 = mysqli_query($con,"SELECT * FROM raspisanie WHERE
instr='" . $instrument . "' AND school IN(" . $array2 . ")
AND type='regular' AND state='1' ORDER by startdate");
Add condition in your where clause.
$res3 = mysqli_query($con,"SELECT * FROM raspisanie WHERE
instr='" . $instrument . "' AND school IN(" . $array2 . ")
AND type='regular' AND state='1' AND startdate >now() ORDER by startdate");

SQL Query using DATE_ADD for time

Hey guys I've tried to figure this out,
I have
$query = "SELECT URL
FROM Matches
WHERE match_date = '" . $currentdate . "'
AND play_datetime < '" . $currenttime . "'
AND DATE_ADD(play_datetime, INTERVAL 3 HOUR) > '" . $currenttime . "'";
I can get the first AND to return, but the second part wont work where i have DATE_ADD, ive tried it by itself to return anything and i cant get it to work!
$current time right now is 06:01:14, its = date(h:i:s);
then in play_datetime it picks up the match_date lists a result, the time in play_datetime is 04:00:00
According to your query :
$currendate have date
and $currenttime have time.
But your column name "play_datetime" suggest that it is having datetime or timestamp .
so if this is right that your query is working but you have to put $currentdatetime instead of $currenttime.
$query = "SELECT URL
FROM Matches
WHERE match_date = '" . $currentdate . "'
AND play_datetime < '" . <timestamp> . "'
AND DATE_ADD(play_datetime, INTERVAL 3 HOUR) > '" . <timstamp> . "'";

Select records from MYSQL database grouped by day

I have a database of records that have a timestamp asscoiated with them. I would like to pull records from the database grouped by day, so if there are other records with the same date (24 hour span) I would like them to be grouped together. Can this be done with MYSQL or will I have to pull the records and organise them into arrays using PHP?
Here is a screenshot of my table:
Here is my model function so far:
public function getUsersMoves($options) {
$query = "SELECT * FROM myzone_user_hr_records";
if(isset($options['GUID'])) {
$query .= " WHERE GUID = '" . $options['GUID'] . "'";
}
if((isset($options['minHR'])) && isset($options['maxHR'])) {
$query .= " AND (hr BETWEEN '" . (int)$options['minHR'] . "' AND '" . (int)$options['maxHR'] . "')";
} else if (isset($options['zone'])) {
$query .= " AND zone = '" . (int)$options['zone'] . "'";
}
if(isset($options['activity'])) {
$query .= " AND title = '" . $options['activity'] . "'";
}
$query .= " ORDER BY time DESC";
$query = $this->db->query($query);
return $query->result_array();
}
And my controller code:
$moves = $this->myzone_model->getUsersMoves($options);
I want the data sorted so that these records are grouped together if they have the same date in the timestamp, for example (2012-11-20).
Thanks
try this :
SELECT *, DATE(time) AS date FROM [whatever you want] GROUP BY date
select * from TABLE where `time` like '2012-11-20%'
The option I have suggested works because the LIKE condition in the WHERE selects all dates from the time field that start with 2012-11-20. That means that it doesn't matter what time it is during the day, it will return all results for that day.
To make this work, you must remember to use LIKE and then add the wildcard at the end - %. You can also add the wildcard at the beginning if you wanted. An example to return all days (11th December) for multiple years would be:
SELECT * FROM TABLE WHERE `time` like '%-12-11%'

Categories