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.
Related
I have a history table with 3700000+ entries. I am using server-side processing in the datatable to initially display 25 records. It is taking lots of time to initially load the datatable even though the query fetches only 25 records.
I am using MySQL database. Now I want to limit the total number of entries from which the data should be processed. I want only past 15 days entries to be considered.
Is there a way by which I can load the table quickly?
There are a number of ways you can limit how much data you get back from a SQL Query.
Using WHERE will allow you to only select entries after a certain date.
$sql = "SELECT * FROM history WHERE date > ".strtotime("-15 days");
The above query assumes that in your history database you have a date that is a unix based timestamp. Another way that you could limit the amount of data that is returned is by using the LIMIT function.
$sql = "SELECT * FROM history ORDER BY id DESC LIMIT 15";
This query assumes that you only have one entry per day, and will limit it to only 15 entries to be displayed.
Along with this, if your history table has a lot of data that doesn't need to be used, then you can only select the columns that you need, doing something like this:
$sql = "SELECT id, time, name FROM history";
SELECT * FROM $table_name WHERE created_date>= DATE(NOW() - INTERVAL 15 DAY)
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
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)
I need to list the records from the current day, in the db the date is in format 02/02/11
Database
09/01/11
13/01/11
18/02/11
19/02/11
20/02/11
...
Question:
How to do using SQL command + PHP?
Current (working...)
$today = date("Y/m/d");
$sql = SELECT * FROM places WHERE STR_TO_DATE(data, '%d/%m/%y') >= '".$today."' ORDER BY DATE_FORMAT(data, '%d/%m/%y') ASC LIMIT 8";
But all records are listed
I would strongly recommend updating your stored values to the standard MySQL date field type - this will greatly simplify any queries you write and enable you to use all the standard MySQL date and time functions.
You can follow the answer here Converting a date in MySQL from string field to find out how to convert your data.
I am developing a PHP application which will handling many company articles.
Now I am creating a page which should order articles BY DATE ie all articles created in a certain day are shown with the appropriate heading and so on for all the articles.
I have used Unix timestamps to save the dates in the MySql db but I cant find code which only sorts dates by days. Can I please get some assistance.
Thanx
You can use mktime() to get the timestamps you would want to use to bin the entries:
http://us.php.net/manual/en/function.mktime.php
$timestamp_for_my_birthday = mktime(0,0,0,10,16,1984);
$timestamp_for_the_next_day = mktime(0,0,0,10,17,1984);
if($time > $timestamp_for_my_birthday && $time < $timestamp_for_the_next_day){
// Time is on my birthday
}
To make this into a MySQL query:
$SQL = "SELECT * FROM dates WHERE date > $timestamp_for_my_birthday AND date < $timestamp_for_the_next_day;";
To order by date I think it would go something like:
SELECT * FROM dates ORDER BY FROM_UNIXTIME(time, '%M %d %Y');
Can you do a
SELECT DATE(FROM_UNIXTIME(my_field_name)) FROM table_x ORDER BY my_field_name
You can order by the timestamp column, but if you want a string representation of which day the record belongs to, you can get any part of the date with the FROM_UNIXTIME(timestamp, 'format') function.
select FROM_UNIXTIME(timestampColumn, '%Y %m %d')
The format values in the second parameter can be adjusted based on the table here:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format