非常全面的php日期時間運算匯總,php日期匯總_PHP教程

來源:互聯網
上載者:User

非常全面的php日期時間運算匯總,php日期匯總


執行個體講解之前,先來介紹幾個核心函數:
mktime 函數
mktime() 函數返回一個日期的 Unix 時間戳記。
參數總是表示 GMT 日期,因此 is_dst 對結果沒有影響。
參數可以從右至左依次空著,空著的參數會被設為相應的當前 GMT 值。
文法:mktime(hour,minute,second,month,day,year,is_dst)
參數 描述
hour 可選。規定小時。
minute 可選。規定分鐘。
second 可選。規定秒。
month 可選。規定用數字表示的月。
day 可選。規定天。
year 可選。規定年。在某些系統上,合法值介於 1901 - 2038 之間。不過在 PHP 5 中已經不存在這個限制了。
is_dst 可選。如果時間在日光節約時間(DST)期間,則設定為1,否則設定為0,若未知,則設定為-1。
自 5.1.0 起,is_dst 參數被廢棄。因此應該使用新的時區處理特性。
例子:mktime() 函數對於日期運算和驗證非常有用。它可以自動校正越界的輸入:

<?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 函數
strtotime() 函數將任何英文文本的日期時間描述解析為 Unix 時間戳記。
文法:strtotime(time,now)
參數 描述
time 規定要解析的時間字串。
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)

執行個體

第一種情況是沒有資料庫,只是得到的日期值進行比較的話,那就得完全用php的時間日期Function Compute了,如下:

比如要計算2015-9-5到2015-9-18還有多少天:

<?php $startdate=strtotime("2015-9-5"); $enddate=strtotime("2015-9-18"); //上面的php時間日期函數已經把日期變成了時間戳記,就是變成了秒。這樣只要讓兩數值相減,然後把秒變成天就可以了,比較的簡單,如下: $days=round(($enddate-$startdate)/3600/24) ; echo $days; //days為得到的天數; ?> 

第二種 孩子的成長

 <? date_default_timezone_set('Asia/Shanghai'); //以上一句為設定時區,其實不設也行,但是zde debug的時候會有提示,說什麼不安全的函數…添上吧。  echo date('Y-m-d H:i:s').' 今天是'.date('Y').'年的第'.date('W').'周';  $stime='2005-11-03 10:08'; echo "

***自出生($stime)以來…:

"; echo "今天是第".Lnbsp(daysofnow($stime),3)."
"; echo "今天是第".Lnbsp(weeksofnow($stime),3)."
"; echo "今天是第".Lnbsp(monthsofnow($stime),3)."個月
"; echo "今天是第".Lnbsp(yearsofnow($stime),3)."
"; /* $output=sprintf(" 今天是第%03d
今天是第< font color=red>%03d
今天是第< font color=red>%03d個月
今天是第< font color=red>%03d
....
".date('w',$ntemp)."
"; 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; } // 下面的函數只是加空格用的,不是核心內容,只為美觀 function Lnbsp($data,$num) { $result=trim($data); for($i=$num;$i>=strlen($data);$i--) { $result=' '.$result; } return $result; } ?>

第三種 情況:明天,下個月和明年的日期,就可以用以下的代碼:

$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.'
'; echo $nextmonth.'
'; echo $nextyear.'
';

第四種情況:工作時間(刨除假日)

<? $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");  //假期日期數組,比方國慶,五一,春節等 $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小時 } echo "開始日期:$startDate
"; echo "結束日期:$endDate
"; echo "共花了".$totalHour."小時"; ?>


第五種情況:給出秒算小時

<?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);  ?> 

以上就是為大家提供的php日期時間運算全部執行個體,希望對大家的學習有所協助。

http://www.bkjia.com/PHPjc/1067825.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1067825.htmlTechArticle非常全面的php日期時間運算匯總,php日期匯總 執行個體講解之前,先來介紹幾個核心函數: mktime 函數 mktime() 函數返回一個日期的 Unix 時間戳記。...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.