国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > PHP > 正文

非常全面的php日期時(shí)間運(yùn)算匯總

2020-03-22 18:03:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
mktime() 函數(shù)返回一個(gè)日期的 Unix 時(shí)間戳。
參數(shù)總是表示 GMT 日期,因此 is_dst 對(duì)結(jié)果沒(méi)有影響。
參數(shù)可以從右到左依次空著,空著的參數(shù)會(huì)被設(shè)為相應(yīng)的當(dāng)前 GMT 值。
語(yǔ)法:mktime(hour,minute,second,month,day,year,is_dst)
參數(shù) 描述
hour 可選。規(guī)定小時(shí)。
minute 可選。規(guī)定分鐘。
second可選。規(guī)定秒。
month 可選。規(guī)定用數(shù)字表示的月。
day可選。規(guī)定天。
year 可選。規(guī)定年。在某些系統(tǒng)上,合法值介于 1901 - 2038 之間。不過(guò)在 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ū)處理特性。
例子:mktime() 函數(shù)對(duì)于日期運(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í)間戳。
語(yǔ)法:strtotime(time,now)
參數(shù) 描述
time 規(guī)定要解析的時(shí)間字符串
now 用來(lái)計(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í)間/日期。
語(yǔ)法
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)
實(shí)例
第一種情況是沒(méi)有數(shù)據(jù)庫(kù),只是得到的日期值進(jìn)行比較的話,那就得完全用php的時(shí)間日期函數(shù)計(jì)算了,如下:比如要計(jì)算2015-9-5到2015-9-18還有多少天:
php $startdate=strtotime("2015-9-5"); $enddate=strtotime("2015-9-18"); //上面的php時(shí)間日期函數(shù)已經(jīng)把日期變成了時(shí)間戳,就是變成了秒。這樣只要讓兩數(shù)值相減,然后把秒變成天就可以了,比較的簡(jiǎn)單,如下: $days=round(($enddate-$startdate)/3600/24) ; echo $days; //days為得到的天數(shù); 第二種 孩子的成長(zhǎng)
date_default_timezone_set('Asia/Shanghai'); //以上一句為設(shè)置時(shí)區(qū),其實(shí)不設(shè)也行,但是zde debug的時(shí)候會(huì)有提示,說(shuō)什么不安全的函數(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 )以來(lái)…: 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/ 今天是第 font color=red b %03d /b /font 周 br/ 今天是第 font color=red b %03d /b /font 個(gè)月 br/ 今天是第 font color=red b %03d /b /font 年 br/ ",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è)月和明年的日期,就可以用以下的代碼:
$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í)間(刨除假日)
$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ù)組,比方國(guó)慶,五一,春節(jié)等 $endWeek=2; //周末是否雙休.雙休為2,僅僅星期天休息為1,沒(méi)有休息為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 "開(kāi)始日期:$startDate BR echo "結(jié)束日期:$endDate BR echo "共花了".$totalHour."小時(shí)";
第五種情況:給出秒算小時(shí) 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教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 绵阳市| 大名县| 德清县| 怀化市| 苏尼特左旗| 镇赉县| 长宁区| 宜州市| 池州市| 高要市| 高陵县| 北海市| 长泰县| 龙泉市| 嘉禾县| 永善县| 屏东市| 普格县| 益阳市| 城步| 资源县| 兴国县| 大安市| 鲁甸县| 石家庄市| 定边县| 台南市| 虹口区| 天峨县| 铜鼓县| 南丹县| 周口市| 汝州市| 舟山市| 苏尼特右旗| 保定市| 绥宁县| 天柱县| 台中县| 孟连| 贺兰县|