MySQL entries sorted by date in wrong order - php

Using a MySQL SELECT query via PHP, I would like to order my entries by date. The query I'm using is:
$query = "SELECT title, file, oblong_pic, square_pic,
DATE_FORMAT(date, '%W %d %M %Y') AS date
FROM dyhamb ORDER BY date DESC";
I have four entries dated: 24/7/2012, 1/7/2012, 5/6/2012 and 10/4/2012 and would like them to be displayed in that order however they are being displayed as: 24/7/2012, 10/4/2010, 5/6/2012 and 1/7/2012.
I can only think that I've set my query up incorrectly somehow but I don't see where I've gone wrong.

%W in DATE_FORMAT is weekday name, so sort starts with that
If you want to use date alias you should rewrite your sql query to:
$query = "SELECT title, file, oblong_pic, square_pic, date AS sort_date,
DATE_FORMAT(date, '%W %d %M %Y') AS date
FROM dyhamb ORDER BY sort_date DESC";

You have defined a column alias with the same name with an existing column. Thus the ordering occurs based on the DATE_FORMAT function, instead of the date real value.
As a good practice, I suggest to remove the DATE_FORMAT function from the mysql query and leave this part to your php script.

DATE_FORMAT returns string, therefore you are ordering it as string, in which you are using %W, so you are ordering firstly by weekday name. If you want to order by date then just do this:
SELECT title, file, oblong_pic, square_pic, date
FROM dyhamb
ORDER BY date DESC
If you really want to select formated date then add DATE_FORMAT(date, '%W %d %M %Y') as formatted_date to selected columns, note that column alias is not date but formatted_date.

Related

how to format date from mysql database inside SQL query

I have a column named date_posted in MySQL database in the form of 2014-11-22 12:45:34 . I need to get only the date, month and year. So, I do
SELECT DATE(date_posted) as date_p FROM tablename
So, I get a format as 2014-11-22. How can I get it in the form 22 Nov, 2014.
And can it still be used for sorting the results.
Thank you :D !
Refering this:
DATE_FORMAT(date_posted, '%d %b, %Y')
And no, this can't be used directly for sorting. You can ,however, parse it to date and then sort later.
order by str_to_date(text_date, '%d %b, %Y')
Try SELECT DATE_FORMAT(date_posted, '%d %b, %Y') as your_date FROM table_name
To get more idea about DATE_FORMAT() refer given link.
http://www.w3schools.com/sql/func_date_format.asp

Formatting a DateTime column in PHP

I have a column that stores DateTime in a MySQL database. I want to compare the Date part (and not the time) to the current day. I am not sure what method to use as I am very new to PHP.
$today=date("Y-m-d");
$sql="SELECT journey_id FROM tbl_journeys WHERE identity='$driver_id' AND DATE_FORMAT(`date`, '%Y %m %d')='$today'";
I tried this way but it's not working. In the statement, date is a column in the table which I need to trim to only Year-month-day
You're using different characters between the year, month, and day in the two formats.
$today=date("Y-m-d");
puts hyphens between them, while
DATE_FORMAT(`date`, '%Y %m %d')
puts spaces between them.
Change one of them to match the other, e.g.
DATE_FORMAT(`date`, '%Y-%m-%d')
Or you could just do it all in MySQL:
$sql="SELECT journey_id FROM tbl_journeys
WHERE identity='$driver_id' AND DATE(`date`) = TODAY()";

PHP MySQL date ordering

Ok im trying to order results by date and time in an ascending order but so far this isnt working :(
$kalendarquery = mysqli_query($con, "SELECT
people.firstname,
people.lastname,
people.id,
people.avatar,
dates.date,
dates.time,
dates.timezonedate
FROM people
INNER JOIN dates ON
people.id=dates.invited_id
WHERE dates.inviter_id='$user_id' AND status='1'
ORDER BY STR_TO_DATE(CONCAT(dates.date,' ',dates.time), '%d/%m/%y %h:%m') ASC
LIMIT 50");
I mean it doesnt order it by the lowest date to the biggest
Your second parameter to STR_TO_DATE has to match the date format in your char field; otherwise, the conversion will just return NULL for every row which is why its not being ordered.
So since your dates are formatted like 27.03.2015 using . as delimeter, you need to make sure you use . in that parameter rather than /.
Also lower case %y is for 2-digit year, so you need %Y because you're using 4-digits:
ORDER BY STR_TO_DATE(CONCAT(dates.date,' ',dates.time), '%d.%m.%Y %h:%i') ASC
You didn't give an example of your time format, but you can figure out how to do it properly by reading the docs for date_format(). But notice above I changed %m to %i in the time part: you were using %m for both month and minutes which is obviously wrong.

Changing date from query with PHP

I have a query (written to be easier from a class)
$cms->my_query('SELECT * FROM location');
Which will return an array
Though I have a DATE type in the mySQL Table which it is formatted like so 2014-06-22
Is there a way I can format so it's like this Nov 04 2008 11:45 PM with using DATE_FORMAT(NOW(),"%b %d %Y %h:%i %p") now I believe DATE cannot use this properly so i'd have to use DATETIME but if that is the case it's fine but how do I select all and change date at the same time?
Example
$cms->my_query('SELECT * FROM location DATE_FORMAT(NOW(),"%b %d %Y %h:%i %p")');
I just don't want that ugly 2014-06-22 and I have very little knowledge of mySQL and I am learning as I try new things out. So if someone who is more skilled please explain the best scenario for me, I'd like to learn and I am willing!
The first argument of DATE_FORMAT() is the date you want to format. Putting NOW() in there means you will return the current date.
First, you'll need to change the date column to DATETIME, then use that column as the first argument to DATE_FORMAT. Try this:
SELECT *, DATE_FORMAT(mydate ,"%b %d %Y %h:%i %p") as date_added FROM location
Where mydate is the DATETIME column from the table.
See demo
The column need to be in type DATETIME. With date_time_column is a column in location table. Should be like this:
$cms->my_query('SELECT DATE_FORMAT(date_time_column,"%m-%d-%Y %r") FROM location');

Date_format - php

When I try to format the date of a datetime field in my mysql db, and echos' the result, like this:
echo $result["date"];
but yet it says for example, 2012-01-03 10:27:53
my script looks like this:
DATE_FORMAT(date, '%a, %b, &Y')
and it should then say 01, 03, 2012 (or something like this)
is it wrong "type" of echo code i use, i am new to the whole date_format thing so i dont really know if im doing it right.
whole query:
SELECT id, subject, DATE_FORMAT(date, '%a, %b, %Y') FROM articles ORDER BY id DESC
No, you're selecting the original date column value, not the value from DATE_FORMAT().
You need to alias that value like this in your SQL query:
DATE_FORMAT(date, '%a, %b, &Y') as formatted_date
And then pick it up in PHP with:
echo $row['formatted_date'];

Categories