Database Migration

I recently migrated from Drupal to Wordpress on this site, and needed to migrate all of my old content to the new database. However, One of the problems with migrating the data from one content management system to another is dealing with the differences in database schema.

Drupal stores content timestamps as an int(11) field, which represents the number of seconds since epoch (midnight, January 1st 1970). Wordpress uses a datetime field. As the two formats are completely different, a conversion must take place. Drupal stores content in the node table, and the field that stores the content creation date is called created. We'll need to select that field, and use the MySQL function FROM_UNIXTIME() to convert the date. Here's how you do it:
mysql> SELECT title,type,FROM_UNIXTIME(created) as created,body FROM
> node ORDER BY created;

+-----------------------------------+--------+---------------------+
| title | type | created |
+-----------------------------------+--------+---------------------+
| Story1 | story | 2005-03-10 11:28:22 |
| Story2 | story | 2005-03-11 16:25:14 |
| A New Story | story | 2005-03-11 16:31:18 |
+-----------------------------------+--------+---------------------+
The FROM_UNIXTIME() function also supports formatting of the result:
mysql> SELECT FROM_UNIXTIME(created, '%Y %D %M %h:%i:%s %x');
+--------------------------------------------------------------+
| FROM_UNIXTIME(created, '%Y %D %M %h:%i:%s %x') |
+--------------------------------------------------------------+
| 2005 10th March 11:05:32 2005 |
+--------------------------------------------------------------+
To reverse the conversion, you would use the UNIX_TIMESTAMP() function to convert your datetime field to a Drupal-style int(11) timestamp.
[tags]MySQL, Drupal, Wordpress, database, migration, data+conversion, date+functions,from_unixtime(),content+management+system[/tags]

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

You a such a smart mother

You a such a smart mother fucker, lol...

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

About Erich

Erich is a web developer and a native New Englander who is passionate about life, the universe, and everything.

He is a Drupal consultant, previously employed as a senior developer at Harvard University, working on the IQSS OpenScholar project.  Prior to joining the team at Harvard, he was the engineering manager at CommonPlaces e-Solutions, in Hampstead, NH, contributing as the lead engineer on the Greenopolis.com and Twolia.com.

Erich is active in the Drupal community, having contributed modules and patches to the community. He presented at DrupalCon in Szeged Hungary, and co-presented at DrupalCon 2009 in Washington, DC.

Erich lives in New Hampshire with his wife, two sons, and three weimaraners.  When not writing code, Erich enjoys landscaping and woodworking.

Faceted search

Categories

Content type

Project types

Artwork Type

Artwork Tags

Recent comments

Activity Stream

August 29, 2011

August 25, 2011

August 24, 2011

August 23, 2011

August 15, 2011

August 11, 2011

August 10, 2011

August 9, 2011

August 4, 2011

August 3, 2011