Sort by newest date using php - php

I have the following date field, I need to sort by newest date.
Please help me to solve this.
tried the following query but it's not getting the correct output.
17/12/2014
26/01/2016
19/11/2014
30/06/2014
I need to sort in the following format :
26/01/2016
17/12/2014
19/11/2014
30/06/2014
Here is my code.
$queryold="SELECT * FROM tablename order by STR_TO_DATE(column name,'%m/%d/%Y')";

your code is not working because you have dd/mm/yyyy format. so you need first date then month in conversation
$queryold="SELECT * FROM tablename order by STR_TO_DATE(column_name,'%d/%m/%Y')";

If your column's type is 'datetime' you just have to run this query:
$query = "SELECT * FROM tablename ORDER BY datecolumn DESC";
If it's a varchar the good query is:
$query = "SELECT * FROM tablename ORDER BY CONVERT(datetime, datecolumn) DESC";

Related

Mysql Get table result between two dates

I know my question is similar to other question already answered but my issue is different because I need some alternative or advice no how to go the other way around.
My issue is: I want to get values between either two dates or one according to what user wants..
When User request data of one day.. php query data successful.. but problem is when data requested is between two dates..
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` >= '$first_date' AND
`meta_transaction_date` <= '$second_date' ");
return $query->result();
I get an empty set...
So I thought may be the values are not submitted correct.. so I echoed the values to see if they are correct or not. I find they are correct...
$first_date = 09/13/2014;
$second_date = 09/19/2014;
But if I try to put the value like
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` >= '09/13/2014' AND
`meta_transaction_date` <= '09/19/2014' ");
return $query->result();
I get my result back correct.. so is there anything am doing it wrong??
Change the type of meta_transaction_date to DATE as that is what it is! Also use the standard 'yyyy-mm-dd' when passing in DATEs.
Your problem probably stems from string ordering of the 'mm/dd/yyyy' US date format which is horrible for coding. If you wish to display the DATE in this format, convert it when SELECTing the final output.
MySQL has a built in function called Between that you can use like this:
SELECT * FROM table_name WHERE date_column BETWEEN 'start_date_parameter' AND 'end_time_parameter'
Try to cast the date first , and then with between statement:
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` BETWEEN
date_format(str_to_date('$first_date', '%d/%m/%Y'), '%Y-%m-%d') AND
date_format(str_to_date('$second_date', '%d/%m/%Y'), '%Y-%m-%d')");
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` >= '09/13/2014'
AND `meta_transaction_date` <= '09/19/2014' ");
Since the above seems to be working fine, the problem is in your code.
$query = $this->db->query("SELECT `meta_transaction_date` FROM meta_receipt_data WHERE
meta_transaction_date BETWEEN "
.date( "Y-M-d", ( strtotime($first_date) ) )." AND "
.date( "Y-M-d", ( strtotime($second_date) ) ) );
A word of advice, do not use queries like SELECT * as they will degrade performance of your application. And I just read in your comment to your own question:
I have set the type as Varchar
Do not do that. It is best to use the DATE type to store dates. You can use the query:
ALTER TABLE `meta_receipt_data`
MODIFY `meta_transaction_date` DATE NOT NULL;`
Well, that is assuming you wish to keep the column to not accept null values.
I found that the value had space before and after so I use $first = trim($first_date); and problem solved...

How to use current date in mysql query?

I have a database that contains a column with type - Date. I also have a query with the date inputted as static which works fine but I would like to use todays date in the query. any recommendations?
Query :
$q = 'SELECT count(ID) as count FROM ORDER WHERE
ASSIGN_TO ='.$db->qstr($person).' AND OPEN_DATE ='.$db->qstr('2014-05-14');
This currently displays count of items after 2014-05-14
You could use the NOW() function that returns the current date. To avoid skewed answered by hours/minutes/seconds, you can use date to extract the date part:
$q = 'SELECT count(ID) as count FROM ORDER WHERE
ASSIGN_TO ='.$db->qstr($person).' AND DATE(OPEN_DATE) = DATE(NOW())';

ORDER BY two dates mysql query

I am going to give order in query like:
SELECT * FROM `sales_report` ORDER BY `cancel_date`, `pur_date`
But its not working could you please check what is wrong in this query because its not ordering dates?
pur_date type is date and cancel_date type is text so is it issue ? I can't change its type.
I echo a different think like:
if(cancel_date != ''){
$transection_date = cancel_date;
}
else {
$transection_date = pur_date;
}
Try to use STR_TO_DATE() function:
SELECT
*
FROM
`sales_report`
ORDER BY
STR_TO_DATE(`cancel_date`, '%Y-%m-%d'), `pur_date`;
As zerkms mentioned in comments: You should use format specifiers, dependent of your text values in cancel_date column.
SELECT *, CAST(`cancel_date` as DateTime) cancelDate
FROM `sales_report`
ORDER BY `cancelDate`, `purDate`
This should work

fetching record according current date

can any one explain how i would display my table record according date, like any user of my website can see their activity of current date only...
while($data=mysql_fetch_array($rs))
{
$data["create_date"]=date("M d,y" , $data[create_date]);
i am using this but it displaying previous date result also
here is my code, i am fetching result from different tables
$SQL="select * from $tab where 1 $con ORDER BY id desc $_REQUEST[sort_mode] $con_limit";
while($data=mysql_fetch_array($rs))
{
$data["create_date"]=date("M d,y" , $data[create_date]);
gri("users","WHERE id='$data[op_id]' ","",$op_name);
gri("patient", " where id ='$data[patient_id]' order by id desc","",$patient);
$data[first_name]=$patient[first_name];
$data[last_name]=$patient[last_name];
$data[age]=$patient[age];$data[sex]=$patient[sex];
$data[mob]=$patient[mob];
$data[op_name]=$op_name[name];
$t->set_var($data);
$t->set_var(array("chk_status_$data[id]_$data[status]"=>"selected",));
$t->parse("ABlockList","AccessBlockList",true);
}
You can also use MySQL for this.
Example
select * from table_name where create_date >= NOW();
OR
SELECT * FROM table_name WHERE DATE(create_date) = CURDATE();
OR
SELECT * FROM table_name WHERE DATE(create_date) = DATE(NOW())
DATE() returns the date without time and NOW() returns the current date & time (note we’ve used the DATE() function in this query to remove the time)

PHP: date between date

I have two dates:
10-11-2010 and 17-11-2010
Now i would like to SELECT all rows with the dates between those two.
How can I do that?
its very simple using between in where clause
, read more
select * from mytable where date between '10-11-2010' and '17-11-2010'
Sounds like a SQL question. Try the between condition.
$datefrom=date('Y-m-d');
$dateto=date('Y-m-d');
$sql = 'select * from mytable where depositdate between $datefrom and $dateto';

Categories