Timestamps to display with AM or PM - mysql

I store articles in a mysql database with their corresponding timestamps.
I would like to format the timestamps using the date(), and display whether it's AM or PM when the article is retrieved from the database. Any help will be highly appreciated.

Assuming you are using MySQL, you need to use date_format():
SELECT DATE_FORMAT(hour_column, '%r');
This gives you a 12 hour formatted time (hh:mm:ss followed by AM or PM), anyway also have a look at the relvant bits of the MySQL Documentation

Based on some of your other questions, you use PHP.
As described on the PHP Date page, you can use a (am/pm) or A (AM/PM) to get what you want:
$showDate = date("Y-m-d h:iA", strtotime("2013-01-19 15:42:00"));
//string(18) "2013-01-19 03:42PM"

Related

How to Convert date, time & timezone to MySql Number (INT) format

I have a plugin that allows adding dates from admin. I am trying to add some data into the database by running queries but I can't quite figure out how it's handling the dates. I know the plugin is saving the dates in (INT).
Example: This is the format used for generating the dates in wp-admin
12-01-2021 23:15 +0300
then end up in MySql database as (INT) value of 1610482500
Basically, all I need is to find how I can convert dates in excel or PHP to convert the dates to integers. I tried Excel Date to Number Conversion but it generates about 5 digits only. Not sure how the date is converting so any clues will be very helpful
Thanks
I have found a way.
<?php
$datetimeStr = '2021-01-12 23:15 +0300';
$datetime = strtotime($datetimeStr);
//Displays 1610482500
echo $datetime;
?>

Converting date format produce by excel to SQL

Asking for any ideas to convert this kind of date in SQL from May-15-2020 18:03 to 'yyyyMMddHHmiss' or 'yyyyMMdd'.
I am trying this query
select from_unixtime(unix_timestamp('May-15-2020 16:03', 'MM-dd-yyyy
HH:mi'), 'yyyyMMdd') from dual
but it wont work.
Use the right right function STR_TO_DATE, and use a format that matches your date rather than something scraped from a previous answer/blog.
Reference manuals of date and time functions are very useful for solving these basic problems.

Trac Reporting export Change history with date time

am trying to export alle the change history of all my trac tickets via a tab delimited text file export.
I successed so far with the following SQL Statement:
SELECT
id AS Ticket,
tc.author as author,
tc.field as field,
tc.newvalue as comments_new
from ticket t
LEFT JOIN ticket_change tc ON ( t.id=tc.ticket )
But I need also the time and date of each entry. There is a field time in the table ticket_change but it is only the time without the date.
Seems like I have to use some functions within the SQL to get what I need. Can anybody help me to get there?
Every Help is very appreciated.
Best Regards
Ali
The time field is an integer representing epoch microseconds. You can use the Trac utility trac.util.datefmt.from_utimestamp to convert the value to datetime format. That only helps if you are working in Python though. In other languages, you can use recipes for converting from unix seconds. Note you must divide the value in Trac by 1e6 first, to convert from microseconds to seconds.

Format date in mysql query that uses cast

I've got this as the select part of my query:
SELECT cast(cast(exp_channel_titles.edit_date as char(14)) as datetime) AS Edit_Date
That takes data from a db in this format 20130501092128 and returns it in this format 2013-05-01 09:21:28
I can only assume it is some kind of magic as i don't fully understand how this works tbh.
But, i need to change the format of the date that it spits out to this format: %d/%m/%Y %k:%i:%s
I can honestly say i have no idea how to do this in that query, i've tried adding it as a param to datetime (is that even a mysql function?!?) but no joy and many other poor attempts that i wont go into.
If anyone can help, i'd be hugely grateful!
MySql automatically converts 20130501092128 to a date and time field, even if it is a VARCHAR or a INT, and you can just use this:
SELECT DATE_FORMAT(exp_channel_titles.edit_date, '%d/%m/%Y %k:%i:%s')
Please see fiddle here.
You can change output format using DATE_FORMAT() function from MySQL. Here is the documentation post about it.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
You can change the output format into whatever format you want, but if you recieve that data into an application, modifies it and return that data to server (editing a row for example). Remember to reformat it into a valid date for MySQL.
If you dont know how to do it, just have to do this into your query:
SELECT DATE_FORMAT(cast(cast(exp_channel_titles.edit_date as char(14))
as datetime), '%e/%m/%Y %k:%i:%s') AS Edit_Date

How to get time (hh:mm:ss) from sql query?

I have Database with date field.
I see the time like: 1900-01-01 13:38:00.000
How i can format it like: 13:38 ?
SELECT convert(varchar, getdate(), 108) outputs as hh:mm:ss.
If you are using SQL server 2008 then,
Suppose your column name is XTime which is holding time in HH-MM-SS.ms formate. If we want to retrive time in HH-MM-SS format then write following query,
Select ID, XDate, Convert(Time(0),XTime,0) -- For HH-MM-SS format
Try this:-
RIGHT(CONVERT(VARCHAR,'DATE',100),7)
For non-military time.
I know I am late. This has already been answered by #Yes - That's Jake. It works perfect. But to help you more, I have a cheat sheet link so that you can write it in any format.
You can visit: Date and Time conversions using SQL cheat sheet
You will find a list of codes and the format that #Yes - That's Jake mentioned in his answer.