Add new comment

Converting MySQL TIMESTAMPs to int(11) dates in Drupal

Submitted by kitt on Wed, 2005-05-18 03:28.

A record's creation date or last modified date is often useful information to have. Drupal uses int(11) to store created time for nodes. When importing from PostNuke or other content management systems, where MySQL TIMESTAMPs are used, you can use MySQL's UNIX_TIMESTAMP function to convert from TIMESTAMP to int(11):

$query = 'SELECT pid, name, UNIX_TIMESTAMP(created_on) 
          FROM {exampletable} WHERE pid = %d';

$headers = array('ID', 'Name', 'Date');

$results = db_query($query, $pid);
if (isset($results) && db_num_rows($results) > 0) {
  while($t = db_fetch_array($results)) {
    $rows[] = array($t['pid'],
                 $t['name'],
                 format_date($t['unix_timestamp(creation_date)'], 'small'));
  }
}

$output .= theme('table', $headers, $rows);

Use Drupal's format_date to format the date in the user's preferred format.

Reply

The content of this field is kept private and will not be shown publicly.
Formatting guidelines : Expand filter description
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.