0

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?

RajSanpui
  • 11,556
  • 32
  • 79
  • 146

1 Answers1

2

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)) 
Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
  • 1
    Thanks it works great. `INSERT INTO commits (file_id, image_dir, modification_time,last_updated_by) SELECT file_id, '/opt/insiteone/images/PHAH00', FROM_UNIXTIME(1508240128), 'jbNotify' FROM vna.files WHERE image_proxy_name='PHAH0006.118';` – RajSanpui Oct 17 '17 at 18:08