xiaoguozi's Blog
Pay it forword - 我并不觉的自豪,我所尝试的事情都失败了······习惯原本生活的人不容易改变,就算现状很糟,他们也很难改变,在过程中,他们还是放弃了······他们一放弃,大家就都是输家······让爱传出去,很困难,也无法预料,人们需要更细心的观察别人,要随时注意才能保护别人,因为他们未必知道自己要什么·····

先来介绍几个核心函数:

 

mktime 函数

mktime() 函数返回一个日期的 Unix 时间戳。

参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。

参数可以从右到左依次空着,空着的参数会被设为相应的当前 GMT 值。

语法

mktime(hour,minute,second,month,day,year,is_dst)

 

Java代码  收藏代码
  1. 参数      描述  
  2. hour    可选。规定小时。  
  3. minute  可选。规定分钟。  
  4. second  可选。规定秒。  
  5. month   可选。规定用数字表示的月。  
  6. day             可选。规定天。  
  7. year    可选。规定年。在某些系统上,合法值介于 1901 - 2038 之间。不过在 PHP 5 中已经不存在这个限制了。  
  8. is_dst  可选。如果时间在日光节约时间(DST)期间,则设置为1,否则设置为0,若未知,则设置为-1。  
  9.   
  10. 自 5.1.0 起,is_dst 参数被废弃。因此应该使用新的时区处理特性。  

 

Java代码  收藏代码
  1. 例子  
  2.   
  3. mktime() 函数对于日期运算和验证非常有用。它可以自动校正越界的输入:  
  4.   
  5. <?php  
  6. echo(date("M-d-Y",mktime(0,0,0,12,36,2001)));  
  7. echo(date("M-d-Y",mktime(0,0,0,14,1,2001)));  
  8. echo(date("M-d-Y",mktime(0,0,0,1,1,2001)));  
  9. echo(date("M-d-Y",mktime(0,0,0,1,1,99)));  
  10. ?>  
  11.   
  12. 输出:  
  13.   
  14. Jan-05-2002  
  15. Feb-01-2002  
  16. Jan-01-2001  
  17. Jan-01-1999   

 

 

 

strtotime 函数

strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。

 

语法

strtotime(time,now)

 

Java代码  收藏代码
  1. 参数      描述  
  2. time    规定要解析的时间字符串。  
  3. now     用来计算返回值的时间戳。如果省略该参数,则使用当前时间。   
 

  一周之后:  strtotime("+1 week") ;

  一周之前:  strtotime("-1 week") ;

  一月之后:  strtotime("+1 months") ;

  一天之后:  strtotime("+1 days") ;      

 

  30秒之后 strtotime( " +30 seconds " );

  20分钟之后 strtotime( " +20 minutes " );

  12个小时之后 strtotime( " +12 hours " );

 

 

date 函数

date() 函数格式化一个本地时间/日期。

语法

date(format,timestamp)

 

 

date_default_timezone_set 函数

date_default_timezone_set() 函数设置用在脚本中所有日期/时间函数的默认时区。

语法

date_default_timezone_set(timezone)
Java代码  收藏代码
  1. 参数      描述  
  2. timezone    必需。时区标识符,比如 "UTC" 或 "Europe/Paris"。  
  3. 合法时区的列表:http://www.php.net/manual/en/timezones.php  
 

实例

 

第一种情况是没有数据库,只是得到的日期值进行比较的话,那就得完全用php的时间日期函数计算了,如下:


比如要计算2009-9-5到2009-9-18还有多少天:

 

Java代码  收藏代码
  1. <?php  
  2. $startdate=strtotime("2009-9-5");  
  3. $enddate=strtotime("2009-9-18");    //上面的php时间日期函数已经把日期变成了时间戳,就是变成了秒。这样只要让两数值相减,然后把秒变成天就可以了,比较的简单,如下:  
  4. $days=round(($enddate-$startdate)/3600/24) ;  
  5. echo $days;     //days为得到的天数;  
  6. ?>  

 

 第二种 孩子的成长

 

Java代码  收藏代码
  1. <?  
  2. date_default_timezone_set('Asia/Shanghai');  
  3. //以上一句为设置时区,其实不设也行,但是zde debug的时候会有提示,说什么不安全的函数…添上吧。  
  4.   
  5. echo date('Y-m-d H:i:s').' 今天是'.date('Y').'年的第'.date('W').'周';  
  6.   
  7. $stime='2005-11-03 10:08';  
  8. echo "<br/><br/>***自出生(<font color=blue>$stime</font>)以来…:<br/><br/>";  
  9. echo "今天是第<font color=red><b>".Lnbsp(daysofnow($stime),3)."</b></font>天<br/>";  
  10. echo "今天是第<font color=red><b>".Lnbsp(weeksofnow($stime),3)."</b></font>周<br/>";  
  11. echo "今天是第<font color=red><b>".Lnbsp(monthsofnow($stime),3)."</b></font>个月<br/>";  
  12. echo "今天是第<font color=red><b>".Lnbsp(yearsofnow($stime),3)."</b></font>年<br/>";  
  13. /* 
  14. $output=sprintf(" 今 天是第<font color=red><b>%03d</b></font>天<br/& gt;今天是第< font color=red><b>%03d</b></font>周< br/>今天是第< font color=red><b>%03d</b></font>个 月<br/>今天是第< font color=red><b>%03d</b></font& gt; 年<br/& gt;",daysofnow($stime),weeksofnow($stime),monthsofnow($stime),yearsofnow($stime)); 
  15. echo $output; 
  16. */  
  17.   
  18. function weeksofnow($stime)  
  19. {  
  20.     $ftime=strtotime($stime);  
  21.     $fweeks=date('w',$ftime);  
  22.     if ($fweeks==0) $fweeks=7;  
  23.     $nweeks=date('w');  
  24.     if ($nweeks==0) $nweeks=7;  
  25.     $ftemp=strtotime(date('Y-m-d 00:00:00',$ftime))-$fweeks*60*60*24;  
  26.     $ntemp=strtotime(date('Y-m-d 00:00:00',time()))+(7-$nweeks)*60*60*24;  
  27.     //echo date('w',$ftemp)."<br/>....<br/>".date('w',$ntemp)."<br/>";  
  28.     return ($ntemp-$ftemp)/60/60/24/7;  
  29. }  
  30.   
  31. function daysofnow($stime)  
  32. {  
  33.     $ftime=strtotime($stime);  
  34.     return ceil(abs((time()-$ftime)/(60*60*24)));  
  35. }  
  36.   
  37. function monthsofnow($stime)  
  38. {  
  39.     $ftime=strtotime($stime);  
  40.     $fmonth=date('m',$ftime);  
  41.     $fyear=date('Y',$ftime);  
  42.     $nmonth=date('m');  
  43.     $nyear=date('Y');  
  44.     $result=($nyear-$fyear)*12+$nmonth-$fmonth+1;  
  45.     return $result;  
  46. }  
  47.   
  48. function yearsofnow($stime)  
  49. {  
  50.     $ftime=strtotime($stime);  
  51.     $fyear=date('Y',$ftime);  
  52.     $nyear=date('Y');  
  53.     return $nyear-$fyear+1;  
  54. }  
  55.   
  56. // 下面的函数只是加空格用的,不是核心内容,只为美观  
  57. function Lnbsp($data,$num)  
  58. {  
  59.     $result=trim($data);  
  60.     for($i=$num;$i>=strlen($data);$i--) {  
  61.         $result='&nbsp;'.$result;  
  62.     }  
  63.     return $result;  
  64. }  
  65. ?>  

 

 第三种  明天,下个月和明年的日期,就可以用以下的代码:

 

Java代码  收藏代码
  1. $tomorrow     = date('Y-m-d',mktime (0,0,0,date("m"),date("d")+1,date("Y")));  
  2. $nextmonth   = date('Y-m',mktime (0,0,0,date("m")+1,date("d")+1,date("Y")));  
  3. $nextyear     = date('Y',mktime (0,0,0,date("m"),date("d"),date("Y")+1));   
  4.   
  5. echo $tomorrow.'<br/>';  
  6. echo $nextmonth.'<br/>';  
  7. echo $nextyear.'<br/>';  

 

第四种 工作时间(刨除假日)

 

 

Java代码  收藏代码
  1. <?  
  2. $startDate="2001-12-12";  
  3. $endDate="2002-11-1";  
  4.   
  5. $holidayArr=array("05-01","05-02","10-01","10-01","10-02","10-03","10-04","10-05","01-26","01-27","01-28","01-29");  
  6.         //假期日期数组,比方国庆,五一,春节等  
  7. $endWeek=2;  
  8.         //周末是否双休.双休为2,仅仅星期天休息为1,没有休息为0  
  9.   
  10. $beginUX=strtotime($startDate);  
  11. $endUX=strtotime($endDate);  
  12.   
  13. for($n=$beginUX;$n<=$endUX;$n=$n+86400){  
  14.     $week=date("w",$n);  
  15.     $MonDay=date("m-d",$n);  
  16.     if($endWeek){//去处周末休息  
  17.         if($endWeek==2){  
  18.             if($week==0||$week==6) continue;  
  19.         }  
  20.         if($endWeek==1){  
  21.             if($week==0) continue;  
  22.         }  
  23.     }  
  24.     if(in_array($MonDay,$holidayArr)) continue;  
  25.     $totalHour+=10;//每天工作10小时  
  26. }  
  27. echo "开始日期:$startDate<BR>";  
  28. echo "结束日期:$endDate<BR>";  
  29. echo "共花了".$totalHour."小时";  
  30. ?>  
 

 

第五种 mysql 处理日期 (防灌水)

 

Java代码  收藏代码
  1. // $ip = getenv('REMOTE_ADDR');  
  2.   
  3. $sql = "SELECT TIME_TO_SEC(NOW())-TIME_TO_SEC(time_at) from test ";  
  4.        //  或者   select   time_to_sec("1980-8-20 12:25:33") - time_to_sec("1997-1-23 6:04:56")  
  5.   
  6.       

 

或者用

 

Java代码  收藏代码
  1. SELECT DATEDIFF('2008-12-30','2008-12-29') AS DiffDate  
  2.   
  3. /* 
  4.  结果: 
  5. DiffDate 
  6. */  
 

或者

 

Java代码  收藏代码
  1. /* 
  2. 假设我们有如下的表: 
  3. OrderId     ProductName     OrderDate 
  4. 1            'Computer'     2008-12-29 16:25:46.635 
  5. */  
  6.   
  7. SELECT OrderId,DATE_ADD(OrderDate,INTERVAL 2 DAY) AS OrderPayDate FROM Orders  
  8.   
  9. /* 
  10. 结果: 
  11. OrderId     OrderPayDate 
  12. 1            2008-12-31 16:25:46.635 
  13. */  

详见:http://www.w3school.com.cn/sql/func_date_format.asp

         http://www.w3school.com.cn/sql/sql_dates.asp

 

 

给出秒算小时 php

Php代码  收藏代码
  1. <?php  
  2. function transform($sec){  
  3.   
  4.     $output = '';  
  5.       
  6.     $hours = floor($sec / 3600);  
  7.     $remainSeconds = $sec % 3600;  
  8.   
  9.     $minutes = floor($remainSeconds / 60);  
  10.     $seconds = $sec - $hours * 3600 - $minutes * 60;  
  11.   
  12.     if($sec >= 3600){   
  13.         $output .= $hours.' h / ';  
  14.         $output .= $minutes.' m / ';  
  15.     }  
  16.   
  17.     if($sec >= 60 && $sec < 3600){  
  18.         $output .= $minutes.' m / ';  
  19.     }  
  20.       
  21.     return $output .= $seconds.' s ';  
  22. }  
  23.   
  24. echo transform(3231803);  
  25.   
  26. ?>  
 

帖子发表于**前 (时间戳与日期换算的应用)

Php代码  收藏代码
  1. <?php  
  2. function units($time){  
  3.   $year   = floor($time / 60 / 60 / 24 / 365);  
  4.   $time  -= $year * 60 * 60 * 24 * 365;  
  5.   $month  = floor($time / 60 / 60 / 24 / 30);  
  6.   $time  -= $month * 60 * 60 * 24 * 30;  
  7.   $week   = floor($time / 60 / 60 / 24 / 7);  
  8.   $time  -= $week * 60 * 60 * 24 * 7;  
  9.   $day    = floor($time / 60 / 60 / 24);  
  10.   $time  -= $day * 60 * 60 * 24;  
  11.   $hour   = floor($time / 60 / 60);  
  12.   $time  -= $hour * 60 * 60;  
  13.   $minute = floor($time / 60);  
  14.   $time  -= $minute * 60;  
  15.   $second = $time;  
  16.   $elapse = '';  
  17.   
  18.   $unitArr = array('年'  =>'year', '个月'=>'month',  '周'=>'week', '天'=>'day',  
  19.                    '小时'=>'hour', '分钟'=>'minute', '秒'=>'second'  
  20.                    );  
  21.   
  22.   foreach ( $unitArr as $cn => $u )  {  
  23.       if ( $$u > 0 )      {  
  24.           $elapse = $$u . $cn;  
  25.           break;  
  26.       }  
  27.   }  
  28.   
  29.   return $elapse;  
  30. }  
  31.   
  32. function stamp($past){  
  33.     date_default_timezone_set("America/New_York"); // 解决php5.1以上时间戳会与实际时间相差8小时,找时区请到http://www.php.net/manual/en/timezones.php  
  34.   
  35.     $year    =(int)substr($past,0,4); // 取得年份   
  36.     $month   =(int)substr($past,5,2); // 取得月份  
  37.     $day     =(int)substr($past,8,2); // 取得几号  
  38.   
  39.     $hour    =(int)substr($past,11,2); // 取得小时  
  40.     $minutes =(int)substr($past,14,2); // 取得分钟  
  41.     $second  =(int)substr($past,17,2); // 取得秒数  
  42.   
  43.     $past = mktime($hour,$minutes,$second,$month,$day,$year);  
  44.     $now  = time();      
  45.     $diff = $now - $past;  
  46.       
  47.     return '发表于' . units($diff) . '前';  
  48. }  
  49.   
  50. $past = '2009-12-24 16:49:00'; // 从数据库得到日期  
  51.   
  52. echo stamp($past);  
  53.   
  54. ?>  
 

两个日期相差的天数

 

#方法一:简单方法

 

Php代码  收藏代码
  1. $olddate = '2010-02-11'; //如果要用mktime函数,则要用explode拆解日期。  
  2. $oldtime = strtotime($olddate);  
  3. $passtime = time()-$oldtime; //经过的时间戳。  
  4. echo '你在网上泡了'.floor($passtime/(24*60*60)).'天了'.'<br />'; //12天。  
 

#方法二:用减去全年天数的时间戳来获取。

 

Php代码  收藏代码
  1. $yDate=1;  
  2. $yDate_Y=date('Y',time())-1; //年份-1,即去年  
  3. $yDateYMD="$yDate_Y-01-01";  
  4. $yYMD=strtotime($yDateYMD); //去年的1月1号时间戳。  
  5. $d=date('L',$yYMD)?366:365; //是否是闰年  
  6. $yYearTime=$d*24*60*60;  
  7.   
  8. $yYear=date('Y-m-d',time()-$yYearTime);  
  9. echo "去年的今天:$yYear<br />"; //2009-02-23  

 

#方法三:用直接截取当前日期的年份减一,但不严谨,没有考虑到闰年。

 

Php代码  收藏代码
  1. #计算60年前的今天。忽略当中经过的闰年。  
  2. $yDate_Y=$yDate_Y-59;  
  3. $md=explode('-',date('Y-m-d'));  
  4. $yYMD="$yDate_Y-{$md[1]}-{$md[2]}";  
  5. echo "60年前的今天:$yYMD <br />"; //1950-02-23  
 

#方法四: 用strtotime()和 GNU日期语法---------推荐!

 

Php代码  收藏代码
  1. //3天后; //当前时间为2010-02-23  
  2. $d=strtotime('3 days');  
  3. echo '3天后'.date('Y-m-d',$d)."<br />";  
  4. //3天前:  
  5. $d=strtotime('-3 days');  
  6. echo '3天前'.date('Y-m-d',$d)."<br />"; //2010-02-20  
  7. //一个月前:  
  8. $d=strtotime('-1 months');  
  9. echo '一个月前'.date('Y-m-d',$d)."<br />"; //2010-01-23  
  10.   
  11. //2个月后:  
  12. $d=strtotime('2 months');  
  13. echo '二个月后'.date('Y-m-d',$d)."<br />"; //2010-04-23  
  14.   
  15. //1年前:  
  16. $d=strtotime('-1 years');  
  17. echo '1年前'.date('Y-m-d',$d)."<br />"; //2009-02-23  
  18.   
  19. //2小时前:  
  20. $d=strtotime('-2 hours');  
  21. 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  
 

重设时间

 

Java代码  收藏代码
  1. //DateTime构造函数:o bject DateTime([string $time [,dateTimeZone $timezone])  
  2. $date = new DateTime('2010-02-23 12:26:36');  
  3. echo $date->format('Y-m-d H:i:s')."<br />"; //和date()函数相同。2010-02-23 12:26:36  
  4.   
  5. //重设时间:  
  6. //1、重设日期: boolean setDate(int year,int month,int day)  
  7. //2、重设时间: boolean setDate(int hour,int minute[,int second])  
  8. $date->setDate(2010,2,28);  
  9. echo $date->format('Y-m-d H:i:s')."<br />"; //2010-02-28 12:26:36  
  10.   
  11. //日期计算,相当于上面的strtotime()  
  12. $date->modify("+7 hours");  
  13. echo $date->format('Y-m-d H:i:s')."<br />"; //2010-02-28 19:26:36  
  14. $date->modify("3 days");  
  15. echo $date->format('Y-m-d H:i:s')."<br />"; //2010-03-03 19:26:36 //从上面被改过的28号开始  
  16.   
  17. /*PHP5在WIN不支持money_format函数? 
  18. setlocale(LC_MONETARY,'zh_CN'); 
  19. echo money_format("%i",786.56);//?Fatal error: Call to undefined function money_format() 
  20. */ 
posted on 2012-03-06 22:39 小果子 阅读(1048) 评论(0)  编辑 收藏 引用 所属分类: 学习笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理