Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to print a list of users added by date in ascending order, but there is an issue of month order, i am using dd-mm-yy date format

What I have tried:

PHP
$query="SELECT * FROM `transactions` WHERE `dealer_id` = '$dealer_id' ORDER BY `_date` ASC";
Posted
Updated 2-Dec-19 20:11pm
Comments
Richard Deeming 28-Sep-17 14:03pm    
Simple: don't store dates as strings. Use the DATE type instead.

MySQL :: MySQL 5.7 Reference Manual :: 11.3 Date and Time Types[^]
Member 13435586 28-Sep-17 14:07pm    
but i need th date format like this => dd-mm-yyyy, but in MySQL date format is mm/dd/yyyy
Richard Deeming 28-Sep-17 14:10pm    
Formatting the date is something you do in the presentation code. The data should be stored using an appropriate type - in this case, the DATE type.

That would immediately solve your problem, because the dates would sort correctly. You then just need to use PHP to format the returned value.
Member 13435586 28-Sep-17 14:11pm    
ok thanks..
Member 13435586 28-Sep-17 14:10pm    
how can i change date format in mysql??

PHP
$query="SELECT * FROM `transactions` WHERE `dealer_id` = '$dealer_id' ORDER BY `_date` ASC";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
SELECT * FROM `tablenam` WHERE MONTH(updated_date) = MONTH(NOW())
 
Share this answer
 
Comments
Maciej Los 3-Dec-19 2:19am    
My vote of 1. Reason: your answer is about where statement, but OP is asking about data sorting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900