我正在尝试更改日期在表单输出中的显示格式。我在互联网上搜索过,我相信这就是我需要做的事情:
SELECT datetime(date_submitted,'%m/%d/%Y') as date_submitted FROM guestquestionnaire
但它给了我一张空白页,好像出了什么问题。有任何想法吗?如果您需要更多代码,请与我们联系。我正在使用PDO,理想情况下我希望日期格式为mm / dd / yyyy。如果我也能得到时间格式为上午11点11分(作为一个例子)那也很棒!!
更新 - 代码:
<?php
try {
$handler = new PDO('mysql:host=localhost;dbname=***', '***', '***');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
die();
}
class guestquestionnaireEntry {
public $id, $date_submitted,
$entry;
public function __construct()
{
$this->entry = "
<tr style='font-size: 8pt;'><td width='60%'><a href=\"?ID={$this->ID}\">ID</a> </td><td width='40%' colspan='2'>{$this->date_submitted}</td></tr>
<table border='1' align='center'>
<tr style='background: #566890; font-size: 8pt; font-weight: bold; color: #fff;'><td colspan='3'>Prior to Arrival</td></tr>
</table>";
}
}
SELECT DATE_FORMAT(date_submitted, '%m/%d/%Y') AS date_submitted FROM guestquestionnaire
// Checks if the submitted is a number. If so, isolates the ID and adds "where" clause
$id = (!empty($_GET['ID']) && is_numeric($_GET['ID']))? " where ID = '".$_GET['ID']."'" : "";
// Add the $id to the end of the string
// A single call would be SELECT * FROM guestquestionnaire where ID = '1'
$query = $handler->query("SELECT * FROM guestquestionnaire{$id}");
$query->setFetchMode(PDO::FETCH_CLASS, 'guestquestionnaireEntry');
while($r = $query->fetch()) {
echo $r->entry, '<br>';
}
?>
答案 0 :(得分:1)
您正在寻找的功能是date_format
:
SELECT DATE_FORMAT(date_submitted, '%m/%d/%Y') AS date_submitted
FROM guestquestionnaire
答案 1 :(得分:0)
尝试:
SELECT date_format(`date_submitted`,'%m/%d/%Y') as `date_submitted` FROM `guestquestionnaire`
现在已发布完整代码,似乎问题不在于sql。我没有测试过以下但它看起来或多或少都没问题,所以我希望它有效。
<?php
try {
$handler = new PDO('mysql:host=localhost;dbname=***', '***', '***');
$handler->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch( PDOException $e ) {
die( $e->getMessage() );
}
class guestquestionnaireEntry {
public $id, $date_submitted,$entry;
public function render(){
return "
<tr style='font-size: 8pt;'>
<td width='60%'>
<a href=\"?id={$this->id}\">ID</a>
</td>
<td width='40%' colspan='2'>{$this->date_submitted}</td>
</tr>
<table border='1' align='center'>
<tr style='background: #566890; font-size: 8pt; font-weight: bold; color: #fff;'>
<td colspan='3'>Prior to Arrival</td>
</tr>
</table>";
}
}
$id = !empty( $_GET['ID'] ) && is_numeric( $_GET['ID'] ) ? "where `ID`='".$_GET['ID']."'" : "";
$sql = trim("select * from `guestquestionnaire` {$id}");
$query = $handler->query( $sql );
$result = $query->fetchAll( PDO::FETCH_CLASS, 'guestquestionnaireEntry' );
foreach( $result as $obj ) echo $obj->render();
?>
答案 2 :(得分:0)
从YourTable
中选择convert(varchar(),YourDateColumn,103)答案 3 :(得分:0)
如果我理解你的问题,你想要的是以DateTime格式检索日期和时间并存储在数据库中的date_sumitted列中。
尝试下面的代码一次..适合我。我希望它会对你有所帮助
$stmt = $db->query("SELECT DATE_FORMAT(date_submitted, '%m/%d/%Y') AS date_submitted FROM guestquestionnaire ");
$row = $stmt->fetchall(PDO::FETCH_ASSOC);
foreach ($row as $key)
{
$date = $key['date_submitted'];
echo $date;
}