I'm not able to get my timestamp to format to my liking. I've tried everything. coding anything with date and time racks my brain. In my report table the column from database 'timestamp' gives me results back like 2016-05-09 13:41:13. I'm trying to get it to format like so. 05-09-2016 1:41:13PM.
my query is like so.
$query = "SELECT DATE_ADD(timestamp, INTERVAL 3 HOUR), timestamp, marketer, facility, name, type, phone, email, fax, description, locality, state, zip, latlng FROM leads WHERE marketer = 'Emily'
ORDER BY DATE_ADD(timestamp, INTERVAL 3 HOUR) DESC";
table result like so:
while( $row = mysqli_fetch_assoc($result) ) {
echo "<tr>";
echo "<td>" . $row['DATE_ADD(timestamp, INTERVAL 3 HOUR)'] . "</td><td>" . $row['marketer'] . "</td><td>" . $row['facility'] . "</td><td>" . $row['name'] . "</td><td>" . $row['type'] . "</td><td>" . $row['phone'] . "</td><td>" . $row['email'] . "</td><td>" . $row['fax'] . "</td><td>" . $row['description'] . "</td><td>" . $row['locality'] . "</td><td>" . $row['state'] . "</td><td>" . $row['zip'] . "</td><td>" . $row['latlng'] . "</td>";
echo "</tr>";
i've read multiple forums and documentation on this but need to keep the 3+ hr interval as well which is where i'm coming into problems.
UPDATE I'm not getting the correct time format but now will will not ORDER BY.
$query = "SELECT DATE_FORMAT( timestamp + INTERVAL 3 HOUR, '%m-%d-%Y %r') AS formatted_ts, marketer, facility, name, type, phone, email, fax, description, locality, state, zip, latlng FROM leads WHERE marketer = 'Emily' ORDER BY DATE_FORMAT( formatted_ts + INTERVAL 3 HOUR, '%m-%d-%Y %r') AS formatted_ts, DESC";
when query no results show. when i take out the order it shows correct format.
First, having a PHP array key like 'DATE_ADD(timestamp, INTERVAL 3 HOUR)'
, even though it works, is not very readable, so instead I recommend aliasing it in the query to something more sensible:
$query = "SELECT DATE_ADD(timestamp, INTERVAL 3 HOUR) as future_date, timestamp, marketer, facility, name, type, phone, email, fax, description, locality, state, zip, latlng FROM leads WHERE marketer = 'Emily'
ORDER BY DATE_ADD(timestamp, INTERVAL 3 HOUR) DESC";
To format that how you like, just use the PHP DateTime class:
while( $row = mysqli_fetch_assoc($result) ) {
$future_date = new DateTime($row['future_date']);
echo $future_date->format('m-d-Y g:i:sA');
}
MySQL provides a function DATE_FORMAT
Reference: http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format
2016-05-09 13:41:13. I'm trying to get it to format like so. 05-09-2016 1:41:13PM.
e.g.
SELECT ...
, timestamp
, DATE_FORMAT( timestamp + INTERVAL 3 HOUR, '%m-%d-%Y %r') AS formatted_ts
, ...
FROM ...
I think %r
may return a two digit hour (with leading zeros). To get single digit your, you may need to specify %l:%i:%s%p
in place of %r
.
The MySQL Reference Manual (link above) gives a complete list of specifiers you can use in the format string.
Also, assigning an alias to the expression will give you a simpler column name to reference from the resultset.
$row['formatted_ts']