<code><span><span><?php </span><span>require</span>(<span>"header.php"</span>); <span>$sql</span> = <span>"SELECT entries.*, catetories.cat FROM entries, catetories WHERE entries.cat_id = catetories.id ORDER BY dateposted DESC LIMIT 1;"</span>; <span>//数据库知识,$sql构建查询语句(排序后,最新日期的数组为第一个,limit 1指只取一个)</span><span>$result</span> = mysql_query(<span>$sql</span>); <span>//把查询语句$sql通过mysql_query()语句发送到数据库,并把结果(表)存在$result中</span><span>$row</span> = mysql_fetch_assoc(<span>$result</span>); <span>//把$result中的表通过mysql_fetch_assoc()方法一个个存在$row数组中</span><span>echo</span><span>"<h2><a href="viewentry.php?id=%22</span>.<span>%24row</span>%5B<span>" id>].<span>"'>"</span>.<span>$row</span>[<span>'subject'</span>].<span>"</span></a></h2> <br>"</span>; <span>echo</span><span>"<i>In <a href="viewcat.php?id=%22</span>.<span>%24row</span>%5B<span>" cat_id>].<span>"'>"</span>.<span>$row</span>[<span>'cat'</span>].<span>"</span></a> - Posted on "</i></span>.<span>$row</span>[<span>'dateposted'</span>].<span>""</span>; <span>//date("D jS F Y g.iA",strtotime($row['dateposted']))</span><span>echo</span><span>"<p>"</p></span>; <span>echo</span> nl2br(<span>$row</span>[<span>'body'</span>]); <span>//这个函数将$row['body']的内容里面的空行转换成html的<br>,最后一行关闭段落标签</span><span>echo</span><span>""</span>; <span>//以上涉及html代码格式,以php读取数据的方式构建html</span><span>//viewentry.php 和 viewcat.php是另外的php网页,与本次测试无关</span><span>require</span>(<span>"footer.php"</span>); <span>?></span></span></span></code>
$row['dateposted']를 직접 사용하면 표시되는 내용은 다음과 같습니다.
date() 및 strtotime() 함수에 의해 처리되는 소스 코드 :
<code><span>echo</span><span>"<i>In <a href="viewcat.php?id=%22</span>.<span>%24row</span>%5B<span>" cat_id>].<span>"'>"</span>.<span>$row</span>[<span>'cat'</span>].<span>"</span></a> - Posted on "</i></span>.date(<span>"D jS F Y g.iA"</span>,strtotime(<span>$row</span>[<span>'dateposted'</span>])).<span>""</span>; </code>
효과 스크린샷:
위 내용은 관련 측면을 포함하여 PHP_SQL_database 읽기의 "날짜" 처리를 소개합니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.