﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-&lt;maque&gt;&lt;font color=blue&gt;楚 &amp;nbsp; 客&lt;/font&gt;&lt;/maque&gt;-随笔分类-英语</title><link>http://www.blogjava.net/nayanliu/category/1313.html</link><description>&lt;marquee direction="up" scrollamount=2 height="120" align="right"&gt;
&lt;font color=green&gt;有关PHP,Python,Java,的东东&lt;/font&gt;
&lt;a href="http://a.netbig.com/account/100333"&gt;我的账本&lt;/a&gt;
&lt;/marquee&gt;</description><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 16:44:10 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 16:44:10 GMT</pubDate><ttl>60</ttl><item><title>PHP中的date()函数</title><link>http://www.blogjava.net/nayanliu/archive/2005/05/18/4812.html</link><dc:creator>楚客</dc:creator><author>楚客</author><pubDate>Wed, 18 May 2005 15:14:00 GMT</pubDate><guid>http://www.blogjava.net/nayanliu/archive/2005/05/18/4812.html</guid><wfw:comment>http://www.blogjava.net/nayanliu/comments/4812.html</wfw:comment><comments>http://www.blogjava.net/nayanliu/archive/2005/05/18/4812.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/nayanliu/comments/commentRss/4812.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nayanliu/services/trackbacks/4812.html</trackback:ping><description><![CDATA[<P>函数原形是<BR>&nbsp;&nbsp;&nbsp; string <B class=methodname>date</B> ( string format [, int timestamp] )<BR>返回 format 格式的时间字符串。参数 format 请参见官方文档<A href="http://php.liukang.com/manual/zh/function.date.php">http://php.liukang.com/manual/zh/function.date.php</A>，一般我们用 "Y-m-d" 或者 "Y-m-d H:i:s"。参数 timestamp 是可选的，留空时等同于 time() 。<BR><BR>简单的例子：<BR>&lt;?php<BR>&nbsp;&nbsp;&nbsp; echo date("Y-m-d");&nbsp;&nbsp;&nbsp; //输出：2005-05-18<BR>?&gt;<BR><BR>更复杂有用的例子：(英文档出自php.net)<BR></P>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><STRONG><SPAN style="COLOR: #000000">carlj&nbsp;at&nbsp;vibez&nbsp;dot&nbsp;ca<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">17</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">Jun</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">2003</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">03</SPAN><SPAN style="COLOR: #000000">:</SPAN><SPAN style="COLOR: #000000">28</SPAN></STRONG><SPAN style="COLOR: #000000">&nbsp;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>Why&nbsp;not&nbsp;</SPAN><SPAN style="COLOR: #0000ff">do</SPAN><SPAN style="COLOR: #000000">&nbsp;something&nbsp;like&nbsp;</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">,&nbsp;to&nbsp;find&nbsp;the&nbsp;number&nbsp;of&nbsp;days&nbsp;</SPAN><SPAN style="COLOR: #0000ff">in</SPAN><SPAN style="COLOR: #000000">&nbsp;a&nbsp;month</SPAN><SPAN style="COLOR: #000000">?</SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>$monthNum&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;date(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;or&nbsp;any&nbsp;value&nbsp;from&nbsp;1-12</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">$year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;date(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Y</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;or&nbsp;any&nbsp;value&nbsp;&gt;=&nbsp;1</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">$numDays&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;date(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">t</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,mktime(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,$monthNum,</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,$year))<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>This&nbsp;will&nbsp;tell&nbsp;you&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;there&nbsp;is&nbsp;</SPAN><SPAN style="COLOR: #000000">28</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">31</SPAN><SPAN style="COLOR: #000000">&nbsp;days&nbsp;</SPAN><SPAN style="COLOR: #0000ff">in</SPAN><SPAN style="COLOR: #000000">&nbsp;a&nbsp;month<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV>
<P>可用该方法求某年某月有多少天。<BR><BR>我综合一下，得到的例子：<BR>&lt;?php<BR>&nbsp;echo date("t",mktime(0,0,0,date("5"),1,date("2005")));&nbsp; //输出：31<BR>?&gt;<BR>这不是写程序的好习惯，我们应当把它写成一个函数，以备将来用。</P>
<DIV style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<DIV><SPAN style="COLOR: #008080">&nbsp;1</SPAN><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">&lt;?</SPAN><SPAN style="COLOR: #000000">php<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">求&nbsp;$y&nbsp;年&nbsp;$m&nbsp;月有多少天的函数</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN><SPAN style="COLOR: #008000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">function</SPAN><SPAN style="COLOR: #000000">&nbsp;days_in_a_month($y,$m)<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">($year</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">1901</SPAN><SPAN style="COLOR: #000000">&nbsp;or&nbsp;$year</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">2038</SPAN><SPAN style="COLOR: #000000">)<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #008000">//超出了PHP的时间范围</SPAN><SPAN style="COLOR: #008000"><BR></SPAN></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$mon</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">date($m);<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$year</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">date($y);<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$mkt</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">mktime(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">,$mon,</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">,$year);<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;date(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">t</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,$mkt);<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR></SPAN><SPAN style="COLOR: #008080">13</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top>}<BR></SPAN><SPAN style="COLOR: #008080">14</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">?&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">15</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN></DIV></DIV>
<P>有趣的是，我发现月份其实可以填大于12的数字，象下面这样：<BR>&lt;?php<BR>echo&nbsp; days_in_a_month(2003,14);&nbsp; //输出：29<BR>?&gt;<BR>你知道，14月就是来年的2月。</P><img src ="http://www.blogjava.net/nayanliu/aggbug/4812.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nayanliu/" target="_blank">楚客</a> 2005-05-18 23:14 <a href="http://www.blogjava.net/nayanliu/archive/2005/05/18/4812.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>