Rumah > Artikel > pembangunan bahagian belakang > PHP_SQL_数据库读取的“日期”处理
<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_数据库读取的“日期”处理,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。