SQL :: 将 [INT] 显示为 [DD/MM/YYYY]
我想以 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需将其通过 date() 函数即可。
Just throw it through the date() function.
就是你所需要的。
语法|:
字符串日期 ( string $format [, int $timestamp = time() ] )
is all you need.
Syntax|:
string date ( string $format [, int $timestamp = time() ] )
您是否尝试过使用日期函数?看起来它正是满足您的需要。
Have you tried using the date function? Looks like it does exactly what you need.
如果我理解正确,您将使用 date() 函数。
您意识到 .= 是一种追加方式,对吧?如果没有更多代码,我不确定您的最终意图是什么,但这应该回答您吗?
更多信息: http://php.net/manual/en/function.date.php< /a>
If i understand you right, you would use the date() function.
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