php 时间操作归类

对于php时间表示有两种:

            一、‘xxxx-xx-xx'这种容易分辨的格式

            二、unix时间戳格式

他们的之间的转换关系是:

常规格式转时间戳

[php] view plain copy
 
 print?
  1. $T='2014-05-24';  
  2. $Tr=strtotime($T);  
  3. echo $Tr;  



  输出结果:$Tr=

        1400889600

           

 unix(时间戳)转常规格式:

[php] view plain copy
 
 print?
  1. $Unix=date('Y-m-d',$Tr);  
  2. echo $Unix;  



输出结果:  $Unix=

                         2014-05-24

PHP时间减法

            
[php] view plain copy
 
 print?
  1. $T1='20140506';  
  2. $T2='2014-05-07';  
  3. $R=strtotime($T2)-strtotime($T1); //月份相减;  
  4. $R1=strtotime($T1)-1;             //减去1秒;  
  5. $R2=strtotime($T1)-60;            //减去1分钟;  
  6. $R3=strtotime($T1)-60*60;         //减去1小时;  
  7. $R4=strtotime($T1)-24*60*60;         //减去1天;  
  8. echo '$R='.$R/(24*60*60).'<br/>';  
  9. echo '$R1='.date('Y-m-d G:i:s',$R1).'<br/>';  
  10. echo '$R2='.date('Y-m-d G:i:s',$R2).'<br/>';  
  11. echo '$R3='.date('Y-m-d G:i:s',$R3).'<br/>';  
  12. 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
 
 
提示:不同形式的时间写法是可以直接运算的比如:$T1='20140506' 和 $T2='2014-05-07'

PHP时间加法

[php] view plain copy
 
 print?
  1. </pre><pre name="code" class="html">$T1='2014-05-31';  
  2. $DT=24*60*60;                        //一天的秒数;  
  3. $T2=strtotime($T1)+$DT;              //增加一天;  
  4. $T3=strtotime($T1)+2*$DT;            //增加两天;  
  5. $T4=strtotime($T1)+40*$DT;           //增加40天;  
  6. $T5=strtotime($T1)+1;                //增加1秒;  
  7. $T6=strtotime($T1)+60;               //增加1分钟;  
  8. $T7=strtotime($T1)+60*60;            //增加1小时;  
  9. echo '$T2='.date('Y-m-d',$T2).'<br/>';  
  10. echo '$T3='.date('Y-m-d',$T3).'<br/>';  
  11. echo '$T4='.date('Y-m-d',$T4).'<br/>';  
  12. echo '$T5='.date('Y-m-d G:i:s',$T5).'<br/>';  
  13. echo '$T6='.date('Y-m-d G:i:s',$T6).'<br/>';  
  14. 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
 
提示: 时间到了月末的话,他会自动增加到月份,这是很有用的,省去了很多麻烦;
 

计算一个月有多少天

[php] view plain copy
 
 print?
  1. $T1='20140501';  
  2. $T2='20140601';  
  3. $TR=(strtotime($T2)-strtotime($T1))/(24*60*60);  
  4. echo $TR;  

输出结果:ans =             
                                       31
原文地址:https://www.cnblogs.com/yzryc/p/6269349.html