函数原形是
    string date ( string format [, int timestamp] )
返回 format 格式的时间字符串。参数 format 请参见官方文档http://php.liukang.com/manual/zh/function.date.php,一般我们用 "Y-m-d" 或者 "Y-m-d H:i:s"。参数 timestamp 是可选的,留空时等同于 time() 。
简单的例子:
<?php
    echo date("Y-m-d");    //输出:2005-05-18
?>
更复杂有用的例子:(英文档出自php.net)
 carlj at vibez dot ca
carlj at vibez dot ca
 17-Jun-2003 03:28
17-Jun-2003 03:28 
 Why not do something like this, to find the number of days in a month?
Why not do something like this, to find the number of days in a month?

 $monthNum = date("n"); // or any value from 1-12
$monthNum = date("n"); // or any value from 1-12
 $year        = date("Y"); // or any value >= 1
$year        = date("Y"); // or any value >= 1
 $numDays  = date("t",mktime(0,0,0,$monthNum,1,$year))
$numDays  = date("t",mktime(0,0,0,$monthNum,1,$year))

 This will tell you if there is 28-31 days in a month
This will tell you if there is 28-31 days in a month


 
可用该方法求某年某月有多少天。
我综合一下,得到的例子:
<?php
 echo date("t",mktime(0,0,0,date("5"),1,date("2005")));  //输出:31
?>
这不是写程序的好习惯,我们应当把它写成一个函数,以备将来用。
 1 <?php
<?php
 2 //求 $y 年 $m 月有多少天的函数
//求 $y 年 $m 月有多少天的函数
 3 function days_in_a_month($y,$m)
function days_in_a_month($y,$m)
 4 {
{
 5 if($year<1901 or $year>2038)
       if($year<1901 or $year>2038)
 6 return;    //超出了PHP的时间范围
                return;    //超出了PHP的时间范围
 7 else  {
      else  {
 8 $mon=date($m);
              $mon=date($m);
 9 $year=date($y);
              $year=date($y);
10 $mkt=mktime(0,0,0,$mon,1,$year);
              $mkt=mktime(0,0,0,$mon,1,$year);
11 return date("t",$mkt);
              return date("t",$mkt);
12 }
      }
13 }
}
14 ?>
?>
15
  
有趣的是,我发现月份其实可以填大于12的数字,象下面这样:
<?php
echo  days_in_a_month(2003,14);  //输出:29
?>
你知道,14月就是来年的2月。
	posted on 2005-05-18 23:14 
楚客 阅读(1733) 
评论(0)  编辑  收藏  所属分类: 
PHP 、
英语