I want to remove the seconds and the result i want as like this
14 july 14 10:44.
2014-07-14 10:44:35
I done with this code as
CONCAT( DATE_FORMAT( `login_timestamp` , '%M %e, %Y' ) , ' ', TIME_FORMAT(`login_timestamp` , '%T' ) )
Is there any other methods?
SELECT DATE_FORMAT(login_timestamp,'%d %M %y %H:%i') FROM table_name;
Reference
Date() will only produce the date
date('$time')
Try the following query
SELECT DATE_FORMAT('2014-07-14 10:44:35', '%d %M %y %l:%i');
Related
My Mysql query
select DATE_FORMAT(max(lastmodified), '%d %M %y , %r') from client_log
and my actuall output is
i want result 15th june 16, 02:17:09PM, i have to apply this ordinal concept in php logic but here i don't know how to perform in date_format() function or is there any function available for mysql in timestamp filed?
The issue is %d and it should be %D
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
select DATE_FORMAT(NOW(), '%D %M %y , %r') ;
+-------------------------------------+
| DATE_FORMAT(NOW(), '%D %M %y , %r') |
+-------------------------------------+
| 27th June 16 , 02:57:00 PM |
+-------------------------------------+
1 row in set (0.04 sec)
Try this Format.
select DATE_FORMAT(max(lastmodified), '%D %M %y , %r') from client_log
I have two STRING fields in my table which make up the date and time. I want to concat those fields, convert them into a date so I can get the values between a chosen time. I think I'm running into a problem with str_to_date format inside my query. Here's what I have in the table
theDate theTime
Mon, Jan 20 7:00 pm
Mon, Jan 20 9:00 pm
Tue, Jan 21 5:00 pm
The PHP:
date_default_timezone_set('EST');
//trying to set up the same format as the concatenated fields will be
$now = date('D, M j g:i a');
include('..//db_connect.php');
try {
$stmt = $conn->query("SELECT * FROM table1 WHERE STR_TO_DATE(CONCAT(theDate, ' ', theTime), '%a, %b %e %l:%i %p')
BETWEEN (STR_TO_DATE('$now', '%a, %b %e %l:%i %p') - INTERVAL 4 HOUR AND STR_TO_DATE('$now', '%a, %b %e %l:%i %p') - INTERVAL 10 HOUR)");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($results);
} catch (Exception $e) {
echo $stmt."<br>".$e->getMessage();
}
What I get is syntax error near 'HOURS AND STR_TO_DATE('Wed, Jan 21 7:07', '%a, %b %e %l %i') - INTERVAL 10 HOURS' at line 2. What's wrong with the query? Also, is there a way to print out the query itself, to see the concatenated values, to see the formatted values?
One thing is wrong is HOURS. Must be HOUR
This is your query:
SELECT *
FROM table1
WHERE STR_TO_DATE(CONCAT(theDate, ' ', theTime), '%a, %b %e %l %i %p')
BETWEEN (STR_TO_DATE('$now', '%a, %b %e %l %i %p') - INTERVAL 4 HOURS AND
STR_TO_DATE('$now', '%a, %b %e %l %i %p') - INTERVAL 10 HOURS)
This doesn't quite look like MySQL syntax. There are several small problems -- the hours and the extra parentheses around the BETWEEN, the BETWEEN valus in the wrong order. Perhaps this does what you want?
SELECT *
FROM table1
WHERE STR_TO_DATE(CONCAT(theDate, ' ', theTime), '%a, %b %e %l %i %p')
BETWEEN date_sub(now(), interval 10 hours) AND
date_sub(now(), interval 4 hours);
You should use native types for dates and times in the database, rather than storing them as strings. If they are strings, don't give them names like "date" and "time" that suggests that they are something they are not.
I think you will need to pass in the year somewhere... also your are missing the colon : on the STR_TO_DATE. It's HOUR in singular
Try like this:
// Assuming $now is in the format '2014 Wed, Jan 21 7:07'
SELECT STR_TO_DATE('$now', '%Y %a, %b %e %l:%i') - INTERVAL 4 HOUR;
As per the second part of the question, I think you could do a separated query like this:
SELECT STR_TO_DATE('$now', '%Y %a, %b %e %l:%i') - INTERVAL 4 HOUR, STR_TO_DATE('$now', '%a, %b %e %l:%i %p') - INTERVAL 10 HOUR;
And then show the results, to see what are the actual values being looked up.
I guess your error is that you are using HOURS instead of HOUR, but I suggest a better solution to use date_sub() and mysql now() methods into something like:
SELECT * FROM table1 WHERE myDate BETWEEN date_sub(now(), INTERVAL 4 HOUR) AND date_sub(now(), INTERVAL 10 HOUR);
An other sugestion is to save datetime fields as datetime and not as string less work after.
Problem
I have the last data entry in date_time for 9th of april 2013 whereas when I try to fetch in descending order from the database it is giving me 8th April 2013. Please see the image and Code below. Any help will be appriciated.
Database
Code
SELECT *
FROM data_feeds
WHERE username = 'davidjhume#gmail.com'
AND gadget_data_type = 'Weighin'
ORDER BY STR_TO_DATE( date_time, '%D, %j %M %Y %H:%i:%s' ) DESC
LIMIT 1
Format string is broken, try:
'%a, %e %b %Y %H:%i:%s'
Your format for str_to_date() is wrong
You say
%D, %j %M %Y %H:%i:%s
So according to you the second value is %j. But according to Mysql docs %j is 'day of year'. https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
edit: you have multiple errors in the format. Try: %a, %e %b %Y %H:%i:%s
Please i need help in displaying the date like 4th july - 20th july, 2012 using the date_time() function. how do i do this?
below is what i thought might work, but it didin't.
$query_rssynod = "SELECT syID, session, year, theme, venue,
DATE_FORMAT(startdate, enddate, '%d %b', '%d %b, %Y.') AS DATE
FROM synod";
Try this,
CONCAT(DATE_FORMAT(FROM_UNIXTIME(startdate), '%D %M'), ' - ', DATE_FORMAT(FROM_UNIXTIME(enddate), '%D %M %Y'))
basically DATE_FORMAT( ) accepts the date to be formatted and the format of the date.
Try this:
SELECT
syID, session, year, theme, venue,
CONCAT(
IF(
YEAR(enddate)=YEAR(startdate),
DATE_FORMAT(startdate, '%D %M'),
DATE_FORMAT(startdate, '%D %M, %Y')
),
' - ',
DATE_FORMAT(enddate, '%D %M, %Y')
) AS `DATE`
FROM synod
You should print the dates in your required format as follows:
CONCAT(DATE_FORMAT(startdate, "%d %b"), ' - ', DATE_FORMAT(enddate, "%d %b"), ', ', DATE_FORMAT(startdate, "%Y"));
I need your help with datetime coversion. Inside my database I have date of comments entered like this:
Datetime: 2012-05-08 14:44:53
How can I make it display something close to this
May 15, 2012 2:44PM
Thanks for your time and patience.
DATE_FORMAT() is the answer to your question. It has several formats of date on this link
SELECT DATE_FORMAT(NOW(), '%M %d, %Y %h:%i %p') as FormattedDate;
View The Output Here [SQLFiddle]
%M Month name (January..December)
%d Day of the month, numeric (00..31)
%Y Year, numeric, four digits
%h Hour (01..12)
%i Minutes, numeric (00..59)
%p AM or PM
You need to use strtotime() to convert to a Unix timestamp. You can then use date() to display the exact format you need. Something like this:
$unix = strtotime($datetime);
echo date(F j Y g:iA, $unix);
You should use MySQL DATE_FORMAT() function.
Reference:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Example query:
SELECT DATE_FORMAT(`date`, '%a %d, %Y %l:%s%p') AS `myDate`;