对于php时间表示有两种:
一、‘xxxx-xx-xx'这种容易分辨的格式
二、unix时间戳格式
他们的之间的转换关系是:
常规格式转时间戳
- $T='2014-05-24';
- $Tr=strtotime($T);
- echo $Tr;
输出结果:$Tr=
1400889600
unix(时间戳)转常规格式:
- $Unix=date('Y-m-d',$Tr);
- echo $Unix;
输出结果: $Unix=
2014-05-24
PHP时间减法
- $T1='20140506';
- $T2='2014-05-07';
- $R=strtotime($T2)-strtotime($T1); //月份相减;
- $R1=strtotime($T1)-1; //减去1秒;
- $R2=strtotime($T1)-60; //减去1分钟;
- $R3=strtotime($T1)-60*60; //减去1小时;
- $R4=strtotime($T1)-24*60*60; //减去1天;
- echo '$R='.$R/(24*60*60).'<br/>';
- echo '$R1='.date('Y-m-d G:i:s',$R1).'<br/>';
- echo '$R2='.date('Y-m-d G:i:s',$R2).'<br/>';
- echo '$R3='.date('Y-m-d G:i:s',$R3).'<br/>';
- echo '$R4='.date('Y-m-d G:i:s',$R4).'<br/>';
输出结果: ans =
$R=1
$R1=2014-05-05 23:59:59
$R2=2014-05-05 23:59:00
$R3=2014-05-05 23:00:00
$R4=2014-05-05 0:00:00
$R1=2014-05-05 23:59:59
$R2=2014-05-05 23:59:00
$R3=2014-05-05 23:00:00
$R4=2014-05-05 0:00:00
提示:不同形式的时间写法是可以直接运算的比如:$T1='20140506' 和 $T2='2014-05-07'
PHP时间加法
- </pre><pre name="code" class="html">$T1='2014-05-31';
- $DT=24*60*60; //一天的秒数;
- $T2=strtotime($T1)+$DT; //增加一天;
- $T3=strtotime($T1)+2*$DT; //增加两天;
- $T4=strtotime($T1)+40*$DT; //增加40天;
- $T5=strtotime($T1)+1; //增加1秒;
- $T6=strtotime($T1)+60; //增加1分钟;
- $T7=strtotime($T1)+60*60; //增加1小时;
- echo '$T2='.date('Y-m-d',$T2).'<br/>';
- echo '$T3='.date('Y-m-d',$T3).'<br/>';
- echo '$T4='.date('Y-m-d',$T4).'<br/>';
- echo '$T5='.date('Y-m-d G:i:s',$T5).'<br/>';
- echo '$T6='.date('Y-m-d G:i:s',$T6).'<br/>';
- echo '$T7='.date('Y-m-d G:i:s',$T7).'<br/>';
输出结果:ans =
$T2=2014-06-01
$T3=2014-06-02
$T4=2014-07-10
$T5=2014-05-31 0:00:01
$T6=2014-05-31 0:01:00
$T7=2014-05-31 1:00:00
$T3=2014-06-02
$T4=2014-07-10
$T5=2014-05-31 0:00:01
$T6=2014-05-31 0:01:00
$T7=2014-05-31 1:00:00
提示: 时间到了月末的话,他会自动增加到月份,这是很有用的,省去了很多麻烦;
计算一个月有多少天
- $T1='20140501';
- $T2='20140601';
- $TR=(strtotime($T2)-strtotime($T1))/(24*60*60);
- echo $TR;
输出结果:ans =
31