I am currently working on a gig guide and would like to format the date as: Day - Month - Date
I'm using the following mysql query -
$query_getEvents = "SELECT gig_guide.date, gig_guide.artist, gig_guide.venue,
gig_guide.tickets, DATE_FORMAT(gig_guide.date, '%M %e, %Y')
FROM gig_guide ORDER BY gig_guide.date ASC";
Unfortunately the DATE_FORMAT is not formatting and keeps returning the standard 0000-00-00 format instead of the way I want it.
Any ideas what may have gone wrong with this code? Or could it be a direct problem with the database itself and the date structure.
If the month is not setted in gig_guide.date, the mysql can't make representation 00.month as a string.
Try it with some date which have month setted.
Related
This function
SELECT HOUR(TIMEDIFF('2020-06-17 12:15:00am','2020-06-17 01:15:00am')) as 'diff'
gives me the difference as
11 hours
while actually it should be
1 hour
. How do I fix this? Please advice.
Thank you.
MySQL doesn't recognize am and pm by default, it parses times in 24-hour format. You need to use STR_TO_DATE() if you want to parse a custom datetime format.
Also, you need to put the later time first.
SELECT HOUR(TIMEDIFF(STR_TO_DATE('2020-06-17 01:15:00am', '%Y-%m-%d %r'),
STR_TO_DATE('2020-06-17 12:15:00am', '%Y-%m-%d %r'))) as 'diff'
Currently, my start_time column was string type.
I want to convert 8:00 AM to 8:00:00 using MySQL.
I have tried like this but it didn't work SELECT STR_TO_DATE('8:00 AM', '%h:%i %p')
Since you are using Laravel, I recommend to use Carbon.
Carbon is an inherited php class from DateTime what makes you able to format times in any way you want and like.
Related to your question:
SELECT STR_TO_DATE("08:00 AM", "%h:%i %p"); // output: 08:00:00
Works fine, so there is something else that causes your problem, but you are giving us not enough information to help you further.
What db are you using?
What version?
what outpout do you get?
etc.
I am trying to run three events in a SELECT. Individually they seem to run fine but when I lump them together they do not work together resulting in error messages.
Trying to:
Convert TZ to local TZ
Change DATE FORMAT to Month Day Time
Select only records with an bor_id >166
Any help with the formatting for this would be GREAT. I can't seem to get it right.
select CONVERT_TZ(`borsignupdate`,'-08:00',##global.time_zone) DATE_FORMAT(MAX(borsignupdate),'%M %e %l:%i%p') AS maxdate
FROM borsurvey
WHERE bor_id>166
You need to put the CONVERT_TZ call in the argument to DATE_FORMAT.
SELECT DATE_FORMAT(CONVERT_TZ(MAX(borsignupdate), '-08:00', ##global.time_zone), '%M %e %l:%i%p') AS maxdate
FROM borsurvey
WHERE bor_id > 166
In your question in the comment, you're missing parentheses around the arguments to DATE_FORMAT, and you have some of the arguments to CONVERT_TZ outside its parentheses. This is basic syntax of 90% of programming languages, I'm not sure why you're having so much trouble with it.
SELECT *, DATE_FORMAT(CONVERT_TZ(borsignupdate, '-08:00', ##global.time_zone), '%M %e %l:%i%p') AS localtime
FROM borsurvey
WHERE bor_id > 166
It's basically the same as the previous query, except without the MAX() call around borsignupdate.
java datetime (date.getTime()) is stored as string in mysql field.
How can we convert this to sql date using sql query. I am using mysql database.
Is there any sql function available?
For example - This is stored (1416231812348) for today's date in db.
Thanks for suggestions.
Java is returning the date as a long, to convert it you can use:
SELECT FROM_UNIXTIME(event_time) FROM MY_TABLE
If you get an error, try the following (after testing, I can see that your data is stored in milliseconds so you need to use this method):
SELECT FROM_UNIXTIME(event_time/1000) FROM MY_TABLE
(Change event_time to be the field name in your table and MY_TABLE to be the table name.)
Here is a SQLFiddle example that shows it working.
Here is an answer that gives you formatting options as well:
http://notsoyellowstickies.blogspot.co.uk/2011/11/converting-long-into-datetime-mysql.html
There is a java.sql package, that has time included. You can send it straight into your database without needing to convert it.
This may be a more pre-emptive solution than converting a date string from Java, into time in MySQL.
A similar question was answered and may be able to help you out here:
A datetime equivalent in java.sql ? (is there a java.sql.datetime ?)
most probably you have recorded from:
System.currentTimeMillis()
so:
select DATE_FORMAT ( from_unixtime( your_table_field / 1000 ) , '%e %b %Y');
you can change the date format as you like.
I am executing this query
SELECT *
FROM temp
WHERE DATE_FORMAT(startTime,'%m/%d/%Y') = '7/15/2012'
and startTime column has this value '2012-07-15 12:00:00'
But this is not returning any results. Can somebody please help?
Change here:
7/15/2012
to:
07/15/2012
According to the documentation for the DATE_FORMAT function, %m is "Month, numeric (00..12)". Note the zero-padding. So you need to write '07/15/2012' rather than '7/15/2012'.
(And in case you're wondering — I have no idea what month #0 is. So far as I'm aware, the months range from 01 to 12. Maybe some locales do have a month #0?)