how to format date from mysql database inside SQL query - php

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

Related

php select records between 2 dates with formatted date column

I need to select orders' records between 2 dates. the date format on the database is looks like :
January 1, 2020 -> using %F %j, %Y format.
my try to get the records is:
$from_date = 'January 1, 2020';
$to_date = 'January 5, 2020';
$sql = "
SELECT *
FROM orders
WHERE order_date STR_TO_DATE(order_date, '%F %j, %Y') between STR_TO_DATE('$from_date', '%F %j, %Y') and STR_TO_DATE('$to_date', '%F %j, %Y')
GROUP
BY order_id
";
the result is null
also I tried to use convert(order_date,'%F %j, %Y') but not work and shows me the same result null
hope I explain the issue clearly.
Thanks
There is no '%F' specifier in MySQL. You probably want %M' instead.
Also, '%j' should be '%e': the latter gives you the number of a day in the month (from 1 to 31), while the former is the number of a day in the year (from 1 to 365 - or 366 on leap years).
Your where clause should look like:
where str_to_date(order_date, '%M %e, %Y')
between str_to_date(:from_date, '%M %e, %Y') and str_to_date(:to_date, '%M %e, %Y')
Notes:
Use prepared statements! Do not concatenate variables in the query string: this is both inefficient (the database needs to parse the statement everytime it is executed with different parameters) and unsafe (your code is opened to SQL injection)
You should really consider using the proper datatype to store your data; storing dates as strings is inefficient (you need to convert the strings whenever you need to filter them) and unsafe (you cannot guarantee data integrity at the time when the data is stored in the table)
select * and group by do not go along well together

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');

Convert datetime to varchar from MSSQL to MYSQL

how to convert this line of MSSQL to MYSQL
SELECT convert(VARCHAR(10), getdate(),106) -- dd mm yyyy
I have no idea how to do it in mysql please help..
I couldn't be sure if this is what you wanted from the question, but you may be able to use this as a possible guideline.
SELECT DATE_FORMAT(NOW(), '%d %m %Y') AS date_variable_name;
You can use DATE_FORMAT for the formatting of date in mysql
select DATE_FORMAT(now(),'%d-%m-%Y') as curr_date
You can use the DATE_FORMAT function of MYSQL
See
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format
mysql> SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y');
-> 'Sunday October 2009'
or in your case
mysql> select DATE_FORMAT(now(),'%d-%m-%Y');

MySQL entries sorted by date in wrong order

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.

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