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.
Related
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');
I have a table column named date_created where I store datetime values in VARCHAR e.g. 16-06-2013 10:49:29
I want to get results through mysql query between tow dates. I am using query like this:
... WHERE date_created BETWEEN '06-08-2013 22:30:18' AND '28-08-2013 22:30:22' ...
This query return results but that result also includes older date records that are between 06 and 28 of every month. I also use < and > but these also did not work.
How can I get results that only include records between '06-08-2013 22:30:18' AND '28-08-2013 22:30:22'
You can use MySQL-function STR_TO_DATE to format strings into DATETIME.
SELECT STR_TO_DATE('06-08-2013 22:30:18', '%d-%m-%Y %H:%i:%s');
# 2013-08-06 22:30:18
I would recommend to reformat all VARCHARs to DATETIMEs with a single UPDATE and fix the code.
you need to use the STR_TO_DATE function to convert your date string into a date that can be used for comparisons.
STR_TO_DATE(date_created, '%d-%m-%Y %H:%i:%s') BETWEEN STR_TO_DATE('06-08-2013 22:30:18', '%d-%m-%Y %H:%i:%s') AND STR_TO_DATE('28-08-2013 22:30:22', '%d-%m-%Y %H:%i:%s')
I have two columns (Date and Time) in database that I would like to get from SQL query with specific date format. It can be two values from sql query or one. Here are the details.
Display Format:
Sunday 03/31/2013 12:13:27 AM
Database Columns:
EndDate (Date yyyy-mm-dd)
EndTime (Time hh:mm:ss)
This is what you need:
DATE_FORMAT(CONCAT(EndDate, EndTime), '%W %m/%d/%Y %r')
The date_format() function gives you everything you need.
Example. Let's say you have columns aDate and aTime in your table, and you need to show the date like 'dd-mm-yyyy' and the time like 'hh:mm'. So:
select
date_format(aDate, '%d-$m-%Y'), date_format(aTime, '%H:%i')
from myTable;
Check the reference manual (Chapter 12 - Functions and operators for MySQL 5.5)
If you need to concatenate the results, then:
select
concat(date_format(aDate, '%d-$m-%Y'), ' ', date_format(aTime, '%H:%i'))
from myTable;
This seems to work for me:
select date_format(concat(enddate,' ',endtime),'%W %m/%d/%Y %r')
from yourtable
SQL Fiddle Demo
Good documentation for date_format: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Actually, I am trying to sort my rows by Date field in which the date is stored in the form of like
DateC
===========
April, 2012
May, 2012
January, 2013
Date
===========
2013-01-03 10:51:23
2013-02-19 10:51:23
2013-03-26 10:51:23
But i am not able to sort it simply by just using ORDER BY datec DESC.
I can't use ORDER BY FIELD (datec, 'December 2012', November 2012'...) because there are two dates stored in my database. One is the timestamp i.e date, and the other is custom date i.e. datec which the user enters by himself. If the datec field is empty, it outputs the date field.
So, what I want is it should order the rows by both the date columns...
Please Help. If you have some questions, please ask mee....
Thanks in advance...
I think this is what you are looking for, using STR_TO_DATE and COALESCE:
select *
from yourtable
order by
COALESCE(STR_TO_DATE(datec, '%M %Y'),date)
SQL Fiddle Demo
Date are stored in string format. so you need to use STR_TO_DATE() function
Try this
SELECT * FROM table ORDER BY STR_TO_DATE(datec, "%M, %Y") DESC
This is one advantage of storing dates as string on the database. The ordering can still be applied provided that you need to use STR_TO_DATE() function to convert the string into valid dates.
SELECT *
FROM tableName
ORDER BY STR_TO_DATE(datec, '%M, %Y') DESC
SQLFiddle Demo
LINK
STR_TO_DATE()
if you have time to alter the schema, the best way to do is to store those dates as DATE or DATETIME on the database. If you worry about the formatting of value during SELECT statement, there is still a function that called DATE_FORMAT() which convert the date into string at your desired format.
DATE_FORMAT()
Example,
SELECT DATE_FORMAT(CURDATE(), '%M, %Y') DateC
will yield
March, 2013
SQLFiddle Demo
UPDATE 1
SELECT *
FROM tableName
ORDER BY STR_TO_DATE(datec, '%M, %Y') DESC,
date DESC
SQLFiddle Demo
I am creating a system which updates the user activity when something is done.
I have a variable $userhistory= 'User edited '.$info.' on July 14 2010 or (07-14-2010);
I want to know how can i get the date automatically. for sql query i am using NOW(), but in a variable like $userhistory how do i get the date and it want it only in either of these forms. not along with the time.
Also, I am updating the column userhistory in the database, which is a text field. Is this the correct way to do it? How can i save only 5 or 10 of the last few updates?
If you are using NOW() when you update or write an entry to the database, the column storing the date (userhistory?) should be of DATETIME type.
Then you'd you'd run your SQL as normal :
SELECT field1, field2, UNIX_TIMESTAMP(userhistory) FROM table;
Then in PHP, use date() on the database result to format it accordingly:
// July 14 2010
date('F j Y', $row['userhistory']);
// 07-14-2010
date('m-d-Y', $row['userhistory']); ,
You can do it without PHP as well.
SELECT
DATE_FORMAT(date_column, '%M %d %Y') AS 'Formatted',
DATE_FORMAT(date_column, '%m-%d-%Y') AS 'Formatted2'
FROM
table;