Converting MySQL TIMESTAMPs to int(11) dates in Drupal

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.

Generating links that open in new pages with Drupal

Creating links that open pages in a new window is very easy with drupal

To generate a link with drupal, use the method l (that's a lowercase L):

$output .= l('Link text', 'desired/path/for/link');

Optional values include an attributes array:

l($text, $path, $attributes = array());

You can include a target value in this attributes array. In HTML, the target is the name of the window. If you use _blank, a new window will open:

$output .= l('Text link', 'my/path', array('target' => '_blank'));

This will produce the HTML:
<a href="my/path" target="_blank">Text link</a>

Syndicate content