SQL ::将[INT]显示为[DD / MM / YYYY]

时间:2011-10-20 19:46:18

标签: php sql date

我想显示$ eventdate(在SQL服务器上保存为 unix time INT值),格式为DD / MM / YYYY (或类似)。< / p>

谢谢!

    ...
    $entry_display .= <<<ENTRY_DISPLAY
    <div class="post">
    $eventdate
    </div>
    ...

编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:编辑:

以下是整个代码:

<?php
class simpleCMS {

  var $host;
  var $username;
  var $password;
  var $table;

  public function display_public() {
    $q = "SELECT * 
          FROM eventsDB
          WHERE eventdate > UNIX_TIMESTAMP()
          ORDER BY eventdate ASC
          LIMIT 30";
    $r = mysql_query($q);

    if ( $r !== false && mysql_num_rows($r) > 0 ) {
      while ( $a = mysql_fetch_assoc($r) ) {
        $title = stripslashes($a['title']);
        $author = stripslashes($a['author']);
        $bodytext = stripslashes($a['bodytext']);
        $eventdate = stripslashes($a['eventdate']);
        $created = stripslashes($a['created']);


        $entry_display .= <<<ENTRY_DISPLAY

    <div class="post">
        <table class="eventstable" cellspacing="0" cellpadding="0">
  <tr>
    <td><img src="media/icons/icon_calendar.gif"/>  <b>$title </b></td>
    <td class="right">echo date("Y-m-d H:i:s", $eventdate); </td>
  </tr>
  <tr>
    <td colspan="2" class="small">$bodytext <i>by $author</i></td>
  </tr>
</table>
    </div>

ENTRY_DISPLAY;
      }
    } else {
      $entry_display = <<<ENTRY_DISPLAY

    <h2> Your brand new Events Page! </h2>
    <p>
      No entries have been made yet.
      Follow my instructions to make a new event!
    </p>

ENTRY_DISPLAY;
    }
    $entry_display .= <<<ADMIN_OPTION

    <p class="admin_link">
      <a href="{$_SERVER['PHP_SELF']}?admin=97538642"></a>
    </p>

ADMIN_OPTION;

    return $entry_display;
  }

  public function display_admin() {
    return <<<ADMIN_FORM

    <form action="{$_SERVER['PHP_SELF']}" method="post">

      <label for="title">Title:</label><br />
      <input name="title" id="title" type="text" maxlength="150" />
      <div class="clear"></div>

      <label for="bodytext">Body Text:</label><br />
      <textarea name="bodytext" id="bodytext"></textarea>
      <div class="clear"></div>

      <label for="author">Author:</label><br />
      <input name="author" id="author" type="text" maxlength="100" />
      <div class="clear"></div>

      <label for="eventdate">Date (DD/MM/YY):</label><br />
      <input name="eventdate" id="eventdate" type="text" maxlength="100" />
      <div class="clear"></div>

      <input type="submit" value="Create This Event!" />
    </form>

    <br />

    <a href="../events.php">Back to Events</a>

ADMIN_FORM;
  }

  public function write($p) {
    if ( $_POST['title'] )
      $title = mysql_real_escape_string($_POST['title']);
    if ( $_POST['bodytext'])
      $bodytext = mysql_real_escape_string($_POST['bodytext']);
    if ( $_POST['author'])
      $author = mysql_real_escape_string($_POST['author']);
    if ( $_POST['eventdate'])
      $eventdate = strtotime($_POST['eventdate']);
    if ( $title && $bodytext && $author ) {
      $created = time();
      $sql = "INSERT INTO eventsDB VALUES('$title','$bodytext','$created','$author','$eventdate')";
      return mysql_query($sql);
    } else {
      return false;
    }
  }

  public function connect() {
    mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
    mysql_select_db($this->table) or die("Could not select database. " . mysql_error());

    return $this->buildDB();
  }

  private function buildDB() {
    $sql = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS eventsDB (
title       VARCHAR(150),
bodytext    TEXT,
created     VARCHAR(100),
author      VARCHAR(100),  
eventdate   INT(15),
)
MySQL_QUERY;

    return mysql_query($sql);
  }
}
?>

4 个答案:

答案 0 :(得分:1)

echo date("Y-m-d H:i:s", $eventdate);

就是你所需要的一切。

语法|: string date ( string $format [, int $timestamp = time() ] )

答案 1 :(得分:1)

将其抛出date()函数。

$string = date("d/m/Y", $eventdate);

答案 2 :(得分:0)

您是否尝试过使用date function?看起来它完全符合您的需求。

答案 3 :(得分:0)

如果我理解你的话,你会使用date()函数。

$entry_display = date('m/d/Y', UNIX_TIMESTAMP_HERE);

你意识到。=是一种追加的方式,对吧?没有更多的代码,我不确定你的最终目的是什么,但那应该回答你?

更多信息:http://php.net/manual/en/function.date.php