PHP 时间换算
<?php
date_default_timezone_set ('Asia/Shanghai');
//echo '<br/>'.date_default_timezone_get ();
echo '<br/>'.date("Y-n-d H-i-s");
$startDate = "2010-06-15 11:49:00";//开始时间
$endDate = "2010-05-05 12:53:00";//结束时间
echo '<br/>//相差的天数';
echo '<br/>'.$startDate.'与'.$endDate.'相差'.abs((strtotime($startDate) - strtotime($endDate)))/(24*3600).'天' ;
echo '<br/>相差的小时';
echo '<br/>'.$startDate.'与'.$endDate.'相差'.abs((strtotime($startDate) - strtotime($endDate)))/(24*3600)*(24).'小时' ;
echo '<br/>相差的分钟';
echo '<br/>'.$startDate.'与'.$endDate.'相差'.abs((strtotime($startDate) - strtotime($endDate)))/(24*3600)*24*(60).'分钟' ;
echo '<br/>相差的秒';
echo '<br/>'.$startDate.'与'.$endDate.'相差'.abs((strtotime($startDate) - strtotime($endDate)))/(24*3600)*24*60*(60).'秒' ;
echo '<br/>判断一天是星期几';
echo '<br/>'.$startDate.'是星期'.date("w",strtotime($startDate));
echo '<br/>//获取某年的第几周的起始和结束日期';
function getWeekDate($week,$year)
{
$timestamp = mktime(0,0,0,1,1,$year);
$dayofweek = date("w",$timestamp);
if( $week != 1)
$distance = ($week-1)*7-$dayofweek+1;
$passed_seconds = $distance * 86400;
$timestamp += $passed_seconds;
$firt_date_of_week = date("Y-m-d",$timestamp);
if($week == 1)
$distance = 7-$dayofweek;
else
$distance = 6;
$timestamp += $distance * 86400;
$last_date_of_week = date("Y-m-d",$timestamp);
return array($firt_date_of_week,$last_date_of_week);
}
$weekDate = getWeekDate(3,date('Y',strtotime($startDate)));
echo '<br/>'.$startDate.'第3个星期起止日期'.$weekDate[0].' 至 '.$weekDate[1];
$startTime = strtotime($startDate);
$endTime = strtotime($endDate);
$time = abs($startTime-$endTime);
$start = 0;
$string = "两个时间相差:";
$y = floor($time/(3600*24*360));
echo '<br/>年:'.$y;
if($start || $y )
{
$start = 1;
$time -= $y*3600*24*360;
$string .= $y."年";
}
$m = floor($time/(3600*24*31));
echo '<br/>月:'.$m;
if($start || $m)
{
$start = 1;
$time -= $m*3600*24*31;
$string .= $m."月";
}
$d = floor($time/(3600*24));
echo '<br/>天:'.$d;
if($start || $d)
{
$start = 1;
$time -= $d*3600*24;
$string .= $d."天";
}
$h = floor($time/(3600));
echo '<br/>小时:'.$h;
if($start || $h)
{
$start = 1;
$time -= $h*3600;
$string .= $h."时";
}
$s = floor($time/(60));
echo '<br/>分:'.$s;
if($start || $s)
{
$start = 1;
$time -= $s*60;
$string .= $s."分";
}
echo '<br/>秒:'.$time.'<br/>';
$string .= "{$time}秒";
echo $string;
?>
以下补充一些:
date_default_timezone_set('PRC'); //默认时区
echo "今天:",date("Y-m-d",time()),"<br>";
echo "今天:",date("Y-m-d",strtotime("18 june 2008")),"<br>";
echo "昨天:",date("Y-m-d",strtotime("-1 day")), "<br>";
echo "明天:",date("Y-m-d",strtotime("+1 day")), "<br>";
echo "一周后:",date("Y-m-d",strtotime("+1 week")), "<br>";
echo "一周零两天四小时两秒后:",date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")), "<br>";
echo "下个星期四:",date("Y-m-d",strtotime("next Thursday")), "<br>";
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."<br>";
echo "一个月前:".date("Y-m-d",strtotime("last month"))."<br>";
echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."<br>";
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>";
如何实现加一个月
echo date("Y-m-d",strtotime("+1months",strtotime("2011-08-04"))); //结果是 2011-09-04
一周,改成 +1week