RajSanpui
RajSanpui

Reputation: 12064

How to store a time_t field to MySQL datetime?

I have extracted the modification time of a file using the struct stat structure:

long modtime = image_stats.st_mtime;

This returns 1508240128.

Now, I wish to store this value into a MySQL table which has datatype as datetime. If I store it directly, it fails saying it is not a datetime format.

How do I store it?

Upvotes: 0

Views: 881

Answers (1)

Raymond Nijland
Raymond Nijland

Reputation: 11602

You can use FROM_UNIXTIME to convert a timestamp into a DATETIME

Query

SELECT FROM_UNIXTIME(1508240128);

Result

FROM_UNIXTIME(1508240128)  
---------------------------
2017-10-17 13:35:28        

as insert query

Query

INSERT INTO 
 [table]
(datetime_column)
VALUES
 (FROM_UNIXTIME(1508240128)) 

Upvotes: 2

Related Questions