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'];
Related
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
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
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');
Is it possible to change the configuration of the date(); that's already been inserted into the database?
For example:
right now the date is displayed in the following way: December 12th 2013 12:28pm
Is there a way to display only the parts i choose from it? like "12 DEC".
Trying to get this done in PHP.
Thanks!
Use the date() function. If you're trying to format the value from a database entry, fetch it and display it using strtoupper(date('j M',strtotime($row['column']))) (which will output the format you're looking for and capitalize everything like the example you provided)
Don't store date/time values (especially verbosely formatted) as strings in the database, because you loose the ability to normally maintain and query your data. Use appropriate date data types for that (DATETIME, TIMESTAMP or even INT if you store Unix epoch time in seconds).
You can change the datatype from VARCHAR to DATETIME in a following way
UPDATE table_name
SET column_name = STR_TO_DATE(column_name, '%M %D, %Y, %h:%i %p');
ALTER TABLE table_name CHANGE column_name column_name DATETIME;
Here is SQLFiddle demo
Now you can properly and easily filter and order your data by date column, e.g.:
SELECT *
FROM table_name
WHERE column_name >= '2013-12-17'
AND column_name < '2013-12-18'
ORDER BY column_name DESC;
Now you can easily present your datetime values as you need with DATE_FORMAT() function e.g.:
SELECT DATE_FORMAT(column_name, '%d %b') formatted_date
FROM table_name;
Sample output:
| FORMATTED_DATE |
|----------------|
| 17 Dec |
| 18 Dec |
Here is SQLFiddle demo
In the meantime if you need an immediate solution you can do
SELECT DATE_FORMAT(STR_TO_DATE(column_name, '%M %D, %Y, %h:%i %p'), '%d %b') formatted_date
FROM table_name;
Here is SQLFiddle demo
To get date with your format, you will try:
<?php
echo date('d M');
?>
But, you need update each row in your table to change their value.
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.