Order mysql query by date and time which are separate columns - php

I wish to order my mySQL query by date and time as if they were a combine column (datetime) however they are a separate column (Date and Time). I need to be able to display all the records from a table that are after the current time (using Date("")).

If we assume you have table t with structure:
id | date | time
Then to order by date and time you do:
SELECT * FROM t ORDER BY date,time
To select everything that's newer than the current time do:
SELECT * FROM T WHERE date > CURDATE() OR (date = CURDATE() AND time > CURTIME())
There's an alternative way to do the second one which is based on Creating DATETIME from DATE and TIME so you can refer to that if you need to.
You can combine the above and do a :
SELECT * FROM T WHERE date > CURDATE() OR (date = CURDATE() AND time > CURTIME()) ORDER BY date,time

Related

Compare date and time to current time in MySQL

I am working on a Office Management System in php and I want to create two appointment datatables. One will show previous appointments in descending order and other will show upcoming appointments in ascending order of date and time. Now in my MySQL Database I have date and time as different parameters. Now should I fetch all entries from database and filter using php and show them in different datatables or should I fetch entries using a filtered query and then show them in different datatables.
I have tried these filtered queries but these are not working:
For Upcoming
$sql = "SELECT * FROM p_appointment WHERE UNIX_TIMESTAMP(STR_TO_DATE(CONCAT(date,' ',time), '%Y-%m-%d %H:%i:%s')) >= UNIX_TIMESTAMP(now()) ORDER BY date ASC
For Previous
$sql = "SELECT * FROM p_appointment WHERE UNIX_TIMESTAMP(STR_TO_DATE(CONCAT(date,' ',time), '%Y-%m-%d %H:%i:%s')) < UNIX_TIMESTAMP(now()) ORDER BY date DESC
date is being stored in format 28/07/2021 and time as 2:25 PM
Any solution using php or MySQL will be helpful.
You can get cuttent_date() and current_time() like this:
$sql = "SELECT * FROM p_appointment WHERE date >= current_date() AND time >= current_time() ORDER BY date,time ASC
Also you shouldn't fetch all data from database if you have a lot of records (>100). It is a good practice to use LIMIT to get only part of records and perform pagination for records.

How to select data from table according to dates in MySQL?

I have a table called tasks. In the index file all the tasks are being listed. There is a column name date in tasks. The date is saved in YYYY-MM-DD format. I want to get all the rows whose date is 2016-numbers-from-0-to-12-numbers-from-0-to-30.
example date is:
2016-12-1
You can simply filter the records by specifying date range.
SELECT *
FROM tasks
WHERE date BETWEEN '2016-01-01' AND '2016-12-31'
ORDER BY date DESC
Update
To get records from current date:
SELECT *
FROM tasks
WHERE date BETWEEN curdate() AND '2016-12-31'
ORDER BY date DESC

PHP Select all from current and future dates only

I need to list and records in a table where "theDate" field is either current or future.
The "theDate" field will contain a date in this format: Year-month-day ... so something like: 2014-12-31
So I have SELECT * FROM events but I need the WHERE part so I can get only current and future dates.
How can I do this?
You can use a MySQL keyword called NOW() and a simple greater than or equal to test
WHERE theDate >= NOW()
SELECT * FROM events
WHERE date_column >= CURDATE();
date_column being the name of your column that contains the date.
CURDATE() returns the current date.

Select MySQL row where column is closest to current date

I have a database with a bunch of rows. One of the columns is "date" which has a date for each row in the format: MM/DD/YYYY H:MM PM/AM
How can I write PHP that selects the row where the date is the most recent date that has passed. Meaning if you have a date of tomorrow, a date of today, and a date of yesterday, it picks the row where the date is of yesterday.
I've already connected to the database:
mysql_connect('localhost','username','pass');
mysql_select_db('db_name');
How do I write a query to grab this?
I would do the filtering in SQL rather than PHP, using a variation of the following query:
SELECT *
FROM myTable
WHERE theDate < CURDATE()
ORDER BY theDate DESC
LIMIT 1
This selects all the rows in the past (theDate < CURDATE()), sorts them in reverse chronological order (ORDER BY theDate DESC), then takes the first record (LIMIT 1).
When you query the database, ORDER BY date DESC LIMIT 1
This will only return the most recent result, and thus the result that is closest to the current date. (This only works if you don't put entries that are dated in the future)

Fetching each records in MySQL table within specific date range

can anyone suggest me way to display all records in MySQL table within a specific date range, and if no records available on the date, it will still display as NULL?
SELECT * FROM table_name WHERE date IS NULL OR date BETWEEN 'date1' AND 'date2';
If you wish to include date2 with the result, use this
SELECT * FROM table_name WHERE date IS NULL OR date BETWEEN 'date1' AND ADDDATE('date2', INTERVAL 1 DAY);

Categories