I have a MySQL DB table with a column named "timestamp"
, a type of timestamp
, and attribute of on update CURRENT_TIMESTAMP
and a default of CURRENT_TIMESTAMP
.
If I add a record to the table, specifying other values, but not the timestamp, then the timestamp is automatically added like 2016-12-28 17:02:26
.
In PHP I query the table using the following query
SELECT * FROM history WHERE user_id = 9 ORDER BY timestamp ASC
The result of the query is saved into $rows
and I use a foreach
to create an array with some of the other values formatted. I am attempting to format the time stamp to UK type 24-hour date time: dd/mm/yy, HH:MM:SS
.
I have tried both the date()
and strftime()
functions as follows:
$formatted_datetime = strftime("%d %m %y %H %M %S", $row["timestamp"]);
$formatted_datetime = date("d/m/y, H:i:s", $row["timestamp"]);
Both of these result in the following notice and the date time being output incorrectly like 01 01 70 00 33 36
:
Notice: A non well formed numeric value encountered in /home/ubuntu/workspace/pset7/public/history.php on line 20
I am new to PHP and MySQL and so far none of the other questions or documentation I have seen have successfully addressed performing this conversion.I do not understand why strftime()
does not work, nor how to do this properly?
TIMESTAMP
due to its limitations: "The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC." dev.mysql.com/doc/refman/5.5/en/datetime.html useDATETIME
"The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'." – InsulateCURRENT_TIMESTAMP
as a DATETIME default):CREATE TRIGGER <triggername> BEFORE INSERT ON <tablename> FOR EACH ROW SET NEW.<datetimefield> = NOW()
– Photogrammetry