在浏览器上显示mysql存储的时间戳值

时间:2012-09-26 19:45:04

标签: php mysql timestamp

我正在尝试在浏览器上显示mysql存储的unix时间戳值(并将它们保存为CSV文件)。值存储为int(11)。问题是当我显示它们时,我会看到'd','P','j','M'和其他用于格式化日期而不是实际时间戳的字符。出了什么问题?

<?php

/* MySQL connection and database connection code */
mysql_query ("SELECT emailaddress, subscribedate FROM email_list_subscribers WHERE subscriberid = '$sid'");

$email_result= mysql_query ("SELECT emailaddress, subscribedate FROM  email_list_subscribers WHERE subscriberid = '$sid'");
if (mysql_num_rows ($email_result) == 0) {
  $email = '';
  $subscribedate = '';
} else {
  $email = mysql_fetch_assoc ($email_result);
  $email =  $email['emailaddress'];
  $subscribedate = (string) ( $email['subscribedate']);
  echo $sid . " : " . $subscribedate . "<br/>";
}

?>

1 个答案:

答案 0 :(得分:1)

您必须将时间戳值转换为日期时间才能显示格式化信息:

    $timestamp = '1348617600';

    $datetime = date("Y-m-d H:i:s", $timestamp);

    echo $datetime;

你也可以使用:

echo strtotime($timestamp);

小心,你必须考虑时区