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');
Related
I have varchar field in database and format of that field is like '10 Feb 2016 08:26 PM'.
Now I want to fetch upcoming data, So how can i get that ?
$ctime=strtotime(date("Y-m-d H:i:s"));
if($type=='1'){
$books = $objData->getAll("select *
from bookings
where custID='".$custID."'
and fromTime>'".$ctime."'");
}
But I am not getting correct result, please help.
First of all, as mentioned in comments you should use proper types for dates. To answer your question, it is still possible to achieve, using STR_TO_DATE mysql function.
$objData->getAll("select * from bookings where custID=".$custID."'
AND unix_timestamp(STR_TO_DATE(fromTime, '%d %b %Y %h:%i %p')) > ".time());
Link: Convert VARCHAR timestamp to TIMESTAMP?
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.
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'];
I've looked through past questions - but couldn't find a good match. How do I return all MySQL rows where the column timestamp is on day X, where day X is contained in a PHP variable the following format: "13 Apr 2011", and the timestamp is in database is in the following format: "2011-02-25 22:00:17"?
SELECT * WHERE timestamp (need to add here)
Thanks!
If the date string (in your PHP variable) has a consistent format, you can use the MySQL function str_to_date to convert it into a DATETIME suitable for comparison.
SELECT * FROM tablename WHERE DATE_FORMAT(timestamp,'%e %b %Y') = 'date coming from php'
(It's the opposite of andri's suggestion, it converts the timestamp into a string for comparision)
What you need is STR_TO_DATE
SELECT * WHERE DATE(`timestamp`) = STR_TO_DATE('$var','%M %d %Y')
More here
I am displaying the contents of a datetime field in mysql in a table with php, and want to show just the date. Is there a way to convert or do this, or must the actual string be editied or trimmed?
Using strtotime...
A simple way is like this:
$date="2009-01-27 14:30:22";
echo strftime("%d %m %Y", strtotime($date));
i.e. turn your mysql date into a time value with strtotime, then reformat it with strftime.
However this only works for dates which can be represented by a unix timestamp - so no dates before 1st Jan 1970 or after 2038
Using PEAR's Date class...
The PEAR Date classes for a more thorough approach which should cope better with historical dates:
$date=new void Date("2009-01-27 14:30:22");
echo $date->format("%d %m %Y");
...or use your DB
Alternatively, have your db format it for you with DATE_FORMAT
SELECT col1,col2,DATE_FORMAT(col2, '%W %M %Y') as col2readable FROM mytable;
You could do it with PHP, but if you don't need the additional baggage, you could consider doing it in your query and return only what you need. This example uses DATE_FORMAT():
"SELECT col1, col2, DATE_FORMAT(dateCol, '%d-%m-%Y') as 'newDate'
FROM tablename"