先來介紹幾個(gè)核心函數(shù):
mktime 函數(shù)
mktime() 函數(shù)返回一個(gè)日期的 Unix 時(shí)間戳。
參數(shù)總是表示 GMT 日期,因此 is_dst 對結(jié)果沒有影響。
參數(shù)可以從右到左依次空著,空著的參數(shù)會(huì)被設(shè)為相應(yīng)的當(dāng)前 GMT 值。
語法
mktime(hour,minute,second,month,day,year,is_dst)
Java代碼

- 參數(shù) 描述
- hour 可選。規(guī)定小時(shí)。
- minute 可選。規(guī)定分鐘。
- second 可選。規(guī)定秒。
- month 可選。規(guī)定用數(shù)字表示的月。
- day 可選。規(guī)定天。
- year 可選。規(guī)定年。在某些系統(tǒng)上,合法值介于 1901 - 2038 之間。不過在 PHP 5 中已經(jīng)不存在這個(gè)限制了。
- is_dst 可選。如果時(shí)間在日光節(jié)約時(shí)間(DST)期間,則設(shè)置為1,否則設(shè)置為0,若未知,則設(shè)置為-1。
-
- 自 5.1.0 起,is_dst 參數(shù)被廢棄。因此應(yīng)該使用新的時(shí)區(qū)處理特性。
Java代碼

- 例子
-
- mktime() 函數(shù)對于日期運(yùn)算和驗(yàn)證非常有用。它可以自動(dòng)校正越界的輸入:
-
- <?php
- echo(date("M-d-Y",mktime(0,0,0,12,36,2001)));
- echo(date("M-d-Y",mktime(0,0,0,14,1,2001)));
- echo(date("M-d-Y",mktime(0,0,0,1,1,2001)));
- echo(date("M-d-Y",mktime(0,0,0,1,1,99)));
- ?>
-
- 輸出:
-
- Jan-05-2002
- Feb-01-2002
- Jan-01-2001
- Jan-01-1999
strtotime 函數(shù)
strtotime() 函數(shù)將任何英文文本的日期時(shí)間描述解析為 Unix 時(shí)間戳。
語法
strtotime(time,now)
Java代碼

- 參數(shù) 描述
- time 規(guī)定要解析的時(shí)間字符串。
- now 用來計(jì)算返回值的時(shí)間戳。如果省略該參數(shù),則使用當(dāng)前時(shí)間。
一周之后: strtotime("+1 week") ;
一周之前: strtotime("-1 week") ;
一月之后: strtotime("+1 months") ;
一天之后: strtotime("+1 days") ;
30秒之后 strtotime( " +30 seconds " );
20分鐘之后 strtotime( " +20 minutes " );
12個(gè)小時(shí)之后 strtotime( " +12 hours " );
date 函數(shù)
date() 函數(shù)格式化一個(gè)本地時(shí)間/日期。
語法
date(format,timestamp)
date_default_timezone_set 函數(shù)
date_default_timezone_set() 函數(shù)設(shè)置用在腳本中所有日期/時(shí)間函數(shù)的默認(rèn)時(shí)區(qū)。
語法
date_default_timezone_set(timezone)
Java代碼

- 參數(shù) 描述
- timezone 必需。時(shí)區(qū)標(biāo)識符,比如 "UTC" 或 "Europe/Paris"。
- 合法時(shí)區(qū)的列表:http://www.php.net/manual/en/timezones.php
實(shí)例
第一種情況是沒有數(shù)據(jù)庫,只是得到的日期值進(jìn)行比較的話,那就得完全用php的時(shí)間日期函數(shù)計(jì)算了,如下:
比如要計(jì)算2009-9-5到2009-9-18還有多少天:
Java代碼

- <?php
- $startdate=strtotime("2009-9-5");
- $enddate=strtotime("2009-9-18"); //上面的php時(shí)間日期函數(shù)已經(jīng)把日期變成了時(shí)間戳,就是變成了秒。這樣只要讓兩數(shù)值相減,然后把秒變成天就可以了,比較的簡單,如下:
- $days=round(($enddate-$startdate)/3600/24) ;
- echo $days; //days為得到的天數(shù);
- ?>
第二種 孩子的成長
Java代碼

- <?
- date_default_timezone_set('Asia/Shanghai');
- //以上一句為設(shè)置時(shí)區(qū),其實(shí)不設(shè)也行,但是zde debug的時(shí)候會(huì)有提示,說什么不安全的函數(shù)…添上吧。
-
- echo date('Y-m-d H:i:s').' 今天是'.date('Y').'年的第'.date('W').'周';
-
- $stime='2005-11-03 10:08';
- echo "<br/><br/>***自出生(<font color=blue>$stime</font>)以來…:<br/><br/>";
- echo "今天是第<font color=red><b>".Lnbsp(daysofnow($stime),3)."</b></font>天<br/>";
- echo "今天是第<font color=red><b>".Lnbsp(weeksofnow($stime),3)."</b></font>周<br/>";
- echo "今天是第<font color=red><b>".Lnbsp(monthsofnow($stime),3)."</b></font>個(gè)月<br/>";
- echo "今天是第<font color=red><b>".Lnbsp(yearsofnow($stime),3)."</b></font>年<br/>";
- /*
- $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>個(gè) 月<br/>今天是第< font color=red><b>%03d</b></font& gt; 年<br/& gt;",daysofnow($stime),weeksofnow($stime),monthsofnow($stime),yearsofnow($stime));
- echo $output;
- */
-
- function weeksofnow($stime)
- {
- $ftime=strtotime($stime);
- $fweeks=date('w',$ftime);
- if ($fweeks==0) $fweeks=7;
- $nweeks=date('w');
- if ($nweeks==0) $nweeks=7;
- $ftemp=strtotime(date('Y-m-d 00:00:00',$ftime))-$fweeks*60*60*24;
- $ntemp=strtotime(date('Y-m-d 00:00:00',time()))+(7-$nweeks)*60*60*24;
- //echo date('w',$ftemp)."<br/>....<br/>".date('w',$ntemp)."<br/>";
- return ($ntemp-$ftemp)/60/60/24/7;
- }
-
- function daysofnow($stime)
- {
- $ftime=strtotime($stime);
- return ceil(abs((time()-$ftime)/(60*60*24)));
- }
-
- function monthsofnow($stime)
- {
- $ftime=strtotime($stime);
- $fmonth=date('m',$ftime);
- $fyear=date('Y',$ftime);
- $nmonth=date('m');
- $nyear=date('Y');
- $result=($nyear-$fyear)*12+$nmonth-$fmonth+1;
- return $result;
- }
-
- function yearsofnow($stime)
- {
- $ftime=strtotime($stime);
- $fyear=date('Y',$ftime);
- $nyear=date('Y');
- return $nyear-$fyear+1;
- }
-
- // 下面的函數(shù)只是加空格用的,不是核心內(nèi)容,只為美觀
- function Lnbsp($data,$num)
- {
- $result=trim($data);
- for($i=$num;$i>=strlen($data);$i--) {
- $result=' '.$result;
- }
- return $result;
- }
- ?>
第三種 明天,下個(gè)月和明年的日期,就可以用以下的代碼:
Java代碼

- $tomorrow = date('Y-m-d',mktime (0,0,0,date("m"),date("d")+1,date("Y")));
- $nextmonth = date('Y-m',mktime (0,0,0,date("m")+1,date("d")+1,date("Y")));
- $nextyear = date('Y',mktime (0,0,0,date("m"),date("d"),date("Y")+1));
-
- echo $tomorrow.'<br/>';
- echo $nextmonth.'<br/>';
- echo $nextyear.'<br/>';
第四種 工作時(shí)間(刨除假日)
Java代碼

- <?
- $startDate="2001-12-12";
- $endDate="2002-11-1";
-
- $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");
- //假期日期數(shù)組,比方國慶,五一,春節(jié)等
- $endWeek=2;
- //周末是否雙休.雙休為2,僅僅星期天休息為1,沒有休息為0
-
- $beginUX=strtotime($startDate);
- $endUX=strtotime($endDate);
-
- for($n=$beginUX;$n<=$endUX;$n=$n+86400){
- $week=date("w",$n);
- $MonDay=date("m-d",$n);
- if($endWeek){//去處周末休息
- if($endWeek==2){
- if($week==0||$week==6) continue;
- }
- if($endWeek==1){
- if($week==0) continue;
- }
- }
- if(in_array($MonDay,$holidayArr)) continue;
- $totalHour+=10;//每天工作10小時(shí)
- }
- echo "開始日期:$startDate<BR>";
- echo "結(jié)束日期:$endDate<BR>";
- echo "共花了".$totalHour."小時(shí)";
- ?>
第五種 mysql 處理日期 (防灌水)
Java代碼

- // $ip = getenv('REMOTE_ADDR');
-
- $sql = "SELECT TIME_TO_SEC(NOW())-TIME_TO_SEC(time_at) from test ";
- // 或者 select time_to_sec("1980-8-20 12:25:33") - time_to_sec("1997-1-23 6:04:56")
-
-
或者用
Java代碼

- SELECT DATEDIFF('2008-12-30','2008-12-29') AS DiffDate
-
- /*
- 結(jié)果:
- DiffDate
- 1
- */
或者
Java代碼

- /*
- 假設(shè)我們有如下的表:
- OrderId ProductName OrderDate
- 1 'Computer' 2008-12-29 16:25:46.635
- */
-
- SELECT OrderId,DATE_ADD(OrderDate,INTERVAL 2 DAY) AS OrderPayDate FROM Orders
-
- /*
- 結(jié)果:
- OrderId OrderPayDate
- 1 2008-12-31 16:25:46.635
- */
詳見:http://www.w3school.com.cn/sql/func_date_format.asp
http://www.w3school.com.cn/sql/sql_dates.asp
給出秒算小時(shí) php
Php代碼

- <?php
- function transform($sec){
-
- $output = '';
-
- $hours = floor($sec / 3600);
- $remainSeconds = $sec % 3600;
-
- $minutes = floor($remainSeconds / 60);
- $seconds = $sec - $hours * 3600 - $minutes * 60;
-
- if($sec >= 3600){
- $output .= $hours.' h / ';
- $output .= $minutes.' m / ';
- }
-
- if($sec >= 60 && $sec < 3600){
- $output .= $minutes.' m / ';
- }
-
- return $output .= $seconds.' s ';
- }
-
- echo transform(3231803);
-
- ?>
帖子發(fā)表于**前 (時(shí)間戳與日期換算的應(yīng)用)
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', '個(gè)月'=>'month', '周'=>'week', '天'=>'day',
- '小時(shí)'=>'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以上時(shí)間戳?xí)c實(shí)際時(shí)間相差8小時(shí),找時(shí)區(qū)請到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); // 取得小時(shí)
- $minutes =(int)substr($past,14,2); // 取得分鐘
- $second =(int)substr($past,17,2); // 取得秒數(shù)
-
- $past = mktime($hour,$minutes,$second,$month,$day,$year);
- $now = time();
- $diff = $now - $past;
-
- return '發(fā)表于' . units($diff) . '前';
- }
-
- $past = '2009-12-24 16:49:00'; // 從數(shù)據(jù)庫得到日期
-
- echo stamp($past);
-
- ?>
兩個(gè)日期相差的天數(shù)
#方法一:簡單方法
Php代碼

- $olddate = '2010-02-11'; //如果要用mktime函數(shù),則要用explode拆解日期。
- $oldtime = strtotime($olddate);
- $passtime = time()-$oldtime; //經(jīng)過的時(shí)間戳。
- echo '你在網(wǎng)上泡了'.floor($passtime/(24*60*60)).'天了'.'<br />'; //12天。
#方法二:用減去全年天數(shù)的時(shí)間戳來獲取。
Php代碼

- $yDate=1;
- $yDate_Y=date('Y',time())-1; //年份-1,即去年
- $yDateYMD="$yDate_Y-01-01";
- $yYMD=strtotime($yDateYMD); //去年的1月1號時(shí)間戳。
- $d=date('L',$yYMD)?366:365; //是否是閏年
- $yYearTime=$d*24*60*60;
-
- $yYear=date('Y-m-d',time()-$yYearTime);
- echo "去年的今天:$yYear<br />"; //2009-02-23
#方法三:用直接截取當(dāng)前日期的年份減一,但不嚴(yán)謹(jǐn),沒有考慮到閏年。
Php代碼

- #計(jì)算60年前的今天。忽略當(dāng)中經(jīng)過的閏年。
- $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天后; //當(dāng)前時(shí)間為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
- //一個(gè)月前:
- $d=strtotime('-1 months');
- echo '一個(gè)月前'.date('Y-m-d',$d)."<br />"; //2010-01-23
-
- //2個(gè)月后:
- $d=strtotime('2 months');
- echo '二個(gè)月后'.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小時(shí)前:
- $d=strtotime('-2 hours');
- echo '目前:'.date('Y-m-d H:i:s',time()).',2小時(shí)前'.date('Y-m-d H:i:s',$d)."<br />"; //目前:2010-02-23 13:38:49,2小時(shí)前2010-02-23 11:38:49
重設(shè)時(shí)間
Java代碼

- //DateTime構(gòu)造函數(shù):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()函數(shù)相同。2010-02-23 12:26:36
-
- //重設(shè)時(shí)間:
- //1、重設(shè)日期: boolean setDate(int year,int month,int day)
- //2、重設(shè)時(shí)間: 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
-
- //日期計(jì)算,相當(dāng)于上面的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函數(shù)?
- setlocale(LC_MONETARY,'zh_CN');
- echo money_format("%i",786.56);//?Fatal error: Call to undefined function money_format()
- */
posted on 2012-03-06 22:39
小果子 閱讀(1057)
評論(0) 編輯 收藏 引用 所屬分類:
學(xué)習(xí)筆記