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

发布于 2024-12-10 23:05:48 字数 4312 浏览 0 评论 0原文

我想以 DD/MM/YYYY 格式(或类似格式)显示 $eventdate(在 SQL Server 上保存为 unix 时间 INT 值)。

谢谢!

    ...
    $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);
  }
}
?>

I want to display $eventdate (saved as a unix time INT value on SQL server), in the format DD/MM/YYYY (or similar).

Thanks!

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

EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:

Heres the whole code:

<?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);
  }
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

留一抹残留的笑 2024-12-17 23:05:48

只需将其通过 date() 函数即可。

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

Just throw it through the date() function.

$string = date("d/m/Y", $eventdate);
才能让你更想念 2024-12-17 23:05:48
echo date("Y-m-d H:i:s", $eventdate);

就是你所需要的。

语法|:
字符串日期 ( string $format [, int $timestamp = time() ] )

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

is all you need.

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

零度° 2024-12-17 23:05:48

您是否尝试过使用日期函数?看起来它正是满足您的需要。

Have you tried using the date function? Looks like it does exactly what you need.

許願樹丅啲祈禱 2024-12-17 23:05:48

如果我理解正确,您将使用 date() 函数。

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

您意识到 .= 是一种追加方式,对吧?如果没有更多代码,我不确定您的最终意图是什么,但这应该回答您吗?

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

If i understand you right, you would use the date() function.

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

You realize .= is a way to append, right? Without more code i am not sure what your final intent is, but that should answer you?

More info: http://php.net/manual/en/function.date.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文