How To Convert DATETIME data in One Field to Unix Timestamp and Add Results To New Field in MySQL Command Line

May 29th, 2012 by Garry Conn
Tags: , , , , , , , , , , , , , , , , , , , , , , ,
Posted in Internet | No Comments »

I have a self-hosted WordPress blog post that got popular and has over 100 comments. I decided to purchase a domain name and use the content from the post to build a single manually coded php page. This is something I do often, but always branch off using a separate WordPress install. Exporting / Importing the comments from the old post into the new one is simple. However, in this case, I decided to just manually code a simple index.php page.

For the comments, I decided to use GentleSource Comments. Comment data is stored in a MySQL database and it’s very lightweight compared to using WordPress. My goal was to then simply export the existing WordPress post comment data into the GentleSource Comments database. That process was easy. Basically, I snagged a copy of the wp_comments table and simply changed the field names to match the appropriate field names of what’s required in the GentleSource Comments database and removed unnecessary fields that were not native to the GentleSource Comments database structure.

Here are some screen shots that offer some visuals on what I am doing.

comment_date field imported from the WordPress wp_comments table

comment_date field imported from the WordPress wp_comments table

comment_timestamp field native to GentleSource Comments c5t_comment table

comment_timestamp field native to GentleSource Comments c5t_comment table

Screen shot showing the results of me manually adding the timestamp into the comment_timestamp fields

comment_timestamp field native to GentleSource Comments c5t_comment table

comment_timestamp field native to GentleSource Comments c5t_comment table

Online Unix Timestamp Conversion Tool

This is the page where I do the online unix timestamp conversions.

Screen shot showing the manual process of adding the unix timestamp in phpMyAdmin

Screen shot showing the manual process of adding the unix timestamp in phpMyAdmin

Screen shot showing the manual process of adding the unix timestamp in phpMyAdmin

The project is a success; however, I have one task left to do — and that is to take all of the DATETIME entries from the comment_date field and convert that data to unix timestamp and add it into a new field labeled comment_timestamp. I can easily do this manually line by line in phpMyAdmin, but I would rather do it quickly in mysql> thru a secure shell. The problem is I just can’t seem to figure out the command. Would someone mind helping me out?

I posted this question on Stack Overflow. You’re welcome to respond using the comment form below, but if possible, please add your answer directly to my question post on Stack Overflow located HERE.

Tags: , , , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply