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.
Related
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.
Hi there please help me if you can. Here is my senario:
I have a MySQL database with a column that holds a date in the form of a varchar. The format of the date is the following 29/05/2014 (i.e. d/m/Y).
I'm trying to compare the value of this column with todays date and return any rows where the date is earlier than todays date.
I'm using a php variable to store todays as follows:
$date = date("d/m/Y");
Here is my SQL query:
SELECT * FROM patients WHERE last_seen < '$date'
What gets returned
So what is returned is very unusual (to me). All records where the last_seen "day" is less than todays "day". It seems to be overlooking the month and year. So in other words if I last_seen = "30/05/2014" and todays date is "29/05/2014" this record is still returned.
Does anyone have any ideas what I might be doing wrong here?
Thanks
You really, really shouldn't store dates in a varchar field - use date or datetime or timestamp data type.
That said, sometimes you don't have control over the database and you have to deal with somebody else's bad design decision. In this case, to compare dates, convert the varchar strings to dates and compare them that way. So, in your case, you can have something like this:
$date = date("d/m/Y");
and then
SELECT * FROM patients WHERE str_to_date('last_seen', '%d/%m/%Y') < str_to_date('$date', '%d/%m/%Y')
or simpler
SELECT * FROM patients WHERE date(last_seen) < current_date
This way you are actually comparing dates and not strings containing dates. Naturally, this assumes that all dates are stored in the same format.
EDIT: I just tested the last option - and, apparently, date('30/05/2014') returns NULL on my system (mysql 5.5 on linux), hence I suggest the best way is
SELECT * FROM patients WHERE str_to_date('last_seen', '%d/%m/%Y') < current_date
You need to store your date as DATE or DATETIME in your database.
Then you can use:
SELECT * FROM patients WHERE DATE(last_seen) < CURRENT_DATE
i've run into the following problem:
I had to migrate a database from MS SQL Server where some fields contained date values that weren't stored as such. For example there was a field "offers" with an "validfrom" and "validto" field. Sadly, they used text fields (varchar) for that type of input and im having a hard time now to filter it properly by date. An example as follows :
datefrom = "21.01.2012"
dateto = "21.05.2012"
Now im trying to sort out the old entries by date using
$curDate = date('d.m.Y'); // Outputs 19.03.2013
in my PHP PDO i use the following query
$query = "SELECT * from mytable where validtill >= '$curDate'"
which would output
$query = "SELECT * from mytable where validtill >= '19.03.2013'"
still i am getting old entries with date entries from 2011. I think i am missing something - maybe i cant compare strings as "date". I tried changing the field from vharchar to date but when i do, the whole imported data gets messed up.
Any advice?
Thanks!
In your question, you say you had to migrate from MS Sql Server -- what database did you migrate to? You need to convert the varchar field to a date to do a date comparison.
Assuming MySQL, then you could use str_to_date. Something like this should work:
select str_to_date(dtfield, '%d.%m.%Y')
from yourtable
where str_to_date(dtfield, '%d.%m.%Y') > str_to_date('1/20/2012', '%m/%d/%Y')
SQL Fiddle Demo
Assuming you're still in SQL Server, then you'd have to strip out each part of the date to put the date in a format SQL Server understands and then use CONVERT to change its datatype to a date:
SELECT CONVERT(datetime,RIGHT(dtfield,4)+SUBSTRING(dtfield,4,2)+LEFT(dtfield,2))
FROM yourtable
WHERE CONVERT(datetime,RIGHT(dtfield,4)+SUBSTRING(dtfield,4,2)+LEFT(dtfield,2)) > '1/20/2012'
More Fiddle
Right. That will do a string comparison and seems like you have the DAY first. Which means march 21, 2013 is greater than march 19, 2014... because the first thing it compares to sort is the day.
This text string comparison would work if you could rearrange the data to Y.m.d, that way 2013 > 2012 > 2011, and 2013-02-xx > 2012-02-xx...
My advice would be to make a new column in your database, and you can use a PHP script to convert and store the new correct values as dates.
Maybe that:
$query = "SELECT CAST(m.field as date) as field, m.* from mytable as m where m.field >='" . date('Y-m-d') . "';";
I am trying to construct a mysql query string to pull out certain records but only if the date in the database is greater than the current date.
So I have this so far and I am not sure if this is a legal syntax...
date_default_timezone_set('America/Los_Angeles');
$current_date = date("Y-m-d");
$sql = "SELECT * FROM `coupons` WHERE status = 1 AND end_date > '$current_date'";
Thanks for your help.
It's legal syntax. You can use one.
I use CURRENT_TIMESTAMP in general, i.e.:
SELECT * FROM `coupons` WHERE status = 1 AND end_date > CURRENT_TIMESTAMP
Do you have a need to compare to the LA timezone? CURRENT_TIMESTAMP will use the local MySQL server time (but that should technically be what the date values are stored as, as well).
Currently I have two tables and the following sql statement which correctly retrieves the items in the events table ordered by their dates in the event_dates table:
SELECT * FROM events, event_dates
WHERE events.id=event_dates.event_id
AND events.preview=0 AND event_dates.start_date>=now()
ORDER BY event_dates.start_date ASC,event_dates.start_time ASC LIMIT 3
Now I want to add an extra AND to make sure only the events on the next weekend are set. The date column is in a standard mysql date format (YYYY-MM-DD). Got stuck on this bit. Cheers.
Use PHP strtotime() to get the start and end timestamp of the weekend:
$we_start=strtotime('next saturday');
$we_end=strtotime('next monday')-1;
Then do a sql query to search for timestamps BETWEEN them.
select * from mytable where UNIX_TIMESTAMP(mydatefield) BETWEEN $we_start AND $we_end
Hope that helps.