php 日期时间运算方法总结

发布时间:2019-09-30编辑:脚本学堂
本文介绍下php日期与时间计算的一些代码,对php时间日期运算方法做个总结,有需要的朋友参考下。
(时间戳与日期换算的应用)
 

复制代码 代码示例:
<?php 
function units($time){ 
  $year   = floor($time / 60 / 60 / 24 / 365); 
  $time  -= $year * 60 * 60 * 24 * 365; 
  $month  = floor($time / 60 / 60 / 24 / 30); 
  $time  -= $month * 60 * 60 * 24 * 30; 
  $week   = floor($time / 60 / 60 / 24 / 7); 
  $time  -= $week * 60 * 60 * 24 * 7; 
  $day    = floor($time / 60 / 60 / 24); 
  $time  -= $day * 60 * 60 * 24; 
  $hour   = floor($time / 60 / 60); 
  $time  -= $hour * 60 * 60; 
  $minute = floor($time / 60); 
  $time  -= $minute * 60; 
  $second = $time; 
  $elapse = ''; 
 
  $unitArr = array('年'  =>'year', '个月'=>'month',  '周'=>'week', '天'=>'day', 
                   '小时'=>'hour', '分钟'=>'minute', '秒'=>'second' 
                   ); 
 
  foreach ( $unitArr as $cn => $u )  { 
      if ( $$u > 0 )      { 
          $elapse = $$u . $cn; 
          break
      } 
  } 
 
  return $elapse; 

 
function stamp($past){ 
    date_default_timezone_set("America/New_York"); // 解决php5.1以上时间戳会与实际时间相差8小时,找时区请到http://www.php.net/manual/en/timezones.php 
 
    $year    =(int)substr($past,0,4); // 取得年份  
    $month   =(int)substr($past,5,2); // 取得月份 
    $day     =(int)substr($past,8,2); // 取得几号 
 
    $hour    =(int)substr($past,11,2); // 取得小时 
    $minutes =(int)substr($past,14,2); // 取得分钟 
    $second  =(int)substr($past,17,2); // 取得秒数 
 
    $past = mktime($hour,$minutes,$second,$month,$day,$year); 
    $now  = time();     
    $diff = $now - $past; 
     
    return '发表于' . units($diff) . '前'; 

 
$past = '2009-12-24 16:49:00'; // 从数据库得到日期 
 
echo stamp($past); 
 
?>  
 

 
两个日期相差的天数
 

复制代码 代码示例:
#方法一:简单方法
$olddate = '2010-02-11'; //如果要用mktime函数,则要用explode拆解日期。 
$oldtime = strtotime($olddate); 
$passtime = time()-$oldtime; //经过的时间戳。 
echo '你在网上泡了'.floor($passtime/(24*60*60)).'天了'.'<br />'; //12天。 
 
#方法二:用减去全年天数的时间戳来获取。
$yDate=1; 
$yDate_Y=date('Y',time())-1; //年份-1,即去年 
$yDateYMD="$yDate_Y-01-01"; 
$yYMD=strtotime($yDateYMD); //去年的1月1号时间戳。 
$d=date('L',$yYMD)?366:365; //是否是闰年 
$yYearTime=$d*24*60*60; 
 
$yYear=date('Y-m-d',time()-$yYearTime); 
echo "去年的今天:$yYear<br />"; //2009-02-23 
 
#方法三:用直接截取当前日期的年份减一,但不严谨,没有考虑到闰年。
 
Php代码  收藏代码
#计算60年前的今天。忽略当中经过的闰年。 
$yDate_Y=$yDate_Y-59; 
$md=explode('-',date('Y-m-d')); 
$yYMD="$yDate_Y-{$md[1]}-{$md[2]}"; 
echo "60年前的今天:$yYMD <br />"; //1950-02-23 
 
#方法四: 用strtotime()和 GNU日期语法---------推荐!
 
Php代码  收藏代码
//3天后; //当前时间为2010-02-23 
$d=strtotime('3 days'); 
echo '3天后'.date('Y-m-d',$d)."<br />"; 
//3天前: 
$d=strtotime('-3 days'); 
echo '3天前'.date('Y-m-d',$d)."<br />"; //2010-02-20 
//一个月前: 
$d=strtotime('-1 months'); 
echo '一个月前'.date('Y-m-d',$d)."<br />"; //2010-01-23 
 
//2个月后: 
$d=strtotime('2 months'); 
echo '二个月后'.date('Y-m-d',$d)."<br />"; //2010-04-23 
 
//1年前: 
$d=strtotime('-1 years'); 
echo '1年前'.date('Y-m-d',$d)."<br />"; //2009-02-23 
 
//2小时前: 
$d=strtotime('-2 hours'); 
echo '目前:'.date('Y-m-d H:i:s',time()).',2小时前'.date('Y-m-d H:i:s',$d)."<br />"; //目前:2010-02-23 13:38:49,2小时前2010-02-23 11:38:49 
 
重设时间
//DateTime构造函数:o bject DateTime([string $time [,dateTimeZone $timezone]) 
$date = new DateTime('2010-02-23 12:26:36'); 
echo $date->format('Y-m-d H:i:s')."<br />"; //和date()函数相同。2010-02-23 12:26:36 
 
//重设时间: 
//1、重设日期: boolean setDate(int year,int month,int day) 
//2、重设时间: boolean setDate(int hour,int minute[,int second]) 
$date->setDate(2010,2,28); 
echo $date->format('Y-m-d H:i:s')."<br />"; //2010-02-28 12:26:36 
 
//日期计算,相当于上面的strtotime() 
$date->modify("+7 hours"); 
echo $date->format('Y-m-d H:i:s')."<br />"; //2010-02-28 19:26:36 
$date->modify("3 days"); 
echo $date->format('Y-m-d H:i:s')."<br />"; //2010-03-03 19:26:36 //从上面被改过的28号开始 
 
/*PHP5在WIN不支持money_format函数?
setlocale(LC_MONETARY,'zh_CN');
echo money_format("%i",786.56);//?Fatal error: Call to undefined function money_format()
*/