您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“php如何計算兩個日期相差幾周”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“php如何計算兩個日期相差幾周”吧!
php計算兩個日期相差幾周的方法:首先創(chuàng)建一個PHP示例文件;然后通過“function format($a,$b){...}”等方法實現(xiàn)計算兩個日期間隔的周數(shù)即可。
本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦
php怎么計算兩個日期相差幾周?
使用PHP實現(xiàn)計算兩個日期間隔的年、月、周、日數(shù):
代碼如下:
<?php function format($a,$b){ //檢查兩個日期大小,默認(rèn)前小后大,如果前大后小則交換位置以保證前小后大 if(strtotime($a)>strtotime($b)) list($a,$b)=array($b,$a); $start = strtotime($a); $stop = strtotime($b); $extend = ($stop-$start)/86400; $result['extends'] = $extend; if($extend<7){ //如果小于7天直接返回天數(shù) $result['daily'] = $extend; }elseif($extend<=31){ //小于28天則返回周數(shù),由于閏年2月滿足了 if($stop==strtotime($a.'+1 month')){ $result['monthly'] = 1; }else{ $w = floor($extend/7); $d = ($stop-strtotime($a.'+'.$w.' week'))/86400; $result['weekly'] = $w; $result['daily'] = $d; } }else{ $y= floor($extend/365); if($y>=1){ //如果超過一年 $start = strtotime($a.'+'.$y.'year'); $a = date('Y-m-d',$start); //判斷是否真的已經(jīng)有了一年了,如果沒有的話就開減 if($start>$stop){ $a = date('Y-m-d',strtotime($a.'-1 month')); $m =11; $y--; } $extend = ($stop-strtotime($a))/86400; } if(isset($m)){ $w = floor($extend/7); $d = $extend-$w*7; }else{ $m = isset($m)?$m:round($extend/30); $stop>=strtotime($a.'+'.$m.'month')?$m:$m--; if($stop>=strtotime($a.'+'.$m.'month')){ $d=$w=($stop-strtotime($a.'+'.$m.'month'))/86400; $w = floor($w/7); $d = $d-$w*7; } } $result['yearly'] = $y; $result['monthly'] = $m; $result['weekly'] = $w; $result['daily'] = isset($d)?$d:null; } return array_filter($result); } print_r(format('2012-10-1','2012-12-15')); ?>
運行結(jié)果:
Array([extends]=>75[monthly]=>2[weekly]=>2)
php 查詢某天所在的周數(shù)及對應(yīng)周的起始日期
代碼如下:
/** * @file * @version 1.1 * @author QQ83989686 * @date 2012-8-7 最后修改時間 * @brief */ //獲取某個日期的 周數(shù)、周對應(yīng)的開始結(jié)束時間 private function getWeekStartEndDay($day) { $g = strftime("%u",strtotime($day)); return array('week_num'=>strftime("%V",strtotime($day)),'week_start_day'=>strftime('%Y-%m-%d',strtotime($day)-($g-1)*86400),'week_start_day_cn'=>strftime('%Y年%m月%d日',strtotime($day)-($g-1)*86400),'week_end_day'=>strftime('%Y-%m-%d',strtotime($day) + (7-$g)*86400),'week_end_day_cn'=>strftime('%Y年%m月%d日',strtotime($day) + (7-$g)*86400)); }
到此,相信大家對“php如何計算兩個日期相差幾周”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。