探討:如何使用PHP實現(xiàn)計算兩個日期間隔的年、月、周、日數(shù)
更新時間:2013年06月13日 15:14:48 作者:
本篇文章是對使用PHP實現(xiàn)計算兩個日期間隔的年、月、周、日數(shù)的方法進行了詳細的分析介紹,需要的朋友參考下
使用PHP實現(xiàn)計算兩個日期間隔的年、月、周、日數(shù):
復(fù)制代碼 代碼如下:
<?php
function format($a,$b){
//檢查兩個日期大小,默認前小后大,如果前大后小則交換位置以保證前小后大
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)周的起始日期
復(fù)制代碼 代碼如下:
/**
* @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));
}
相關(guān)文章
php生成SessionID和圖片校驗碼的思路和實現(xiàn)代碼
做一個后臺登陸需要用到校驗碼,前臺的用戶跟蹤需要用到SessionID,當然,默認的PHP開啟了Session以后就有了一個SessionID,但是我需要自己的,并且能夠存儲進數(shù)據(jù)庫,那么我就嘗試了一下,構(gòu)造了以下的函數(shù)。2009-03-03PHP ajax 異步執(zhí)行不等待執(zhí)行結(jié)果的處理方法
這篇文章主要介紹了PHP ajax 異步執(zhí)行不等待執(zhí)行結(jié)果的處理方法,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-05-05PHP實現(xiàn)簡單的協(xié)程任務(wù)調(diào)度demo示例
這篇文章主要介紹了PHP實現(xiàn)簡單的協(xié)程任務(wù)調(diào)度demo,結(jié)合實例形式詳細分析了PHP基于協(xié)程的任務(wù)調(diào)度基本原理、定義及使用技巧,需要的朋友可以參考下2020-02-02WordPress中給文章添加自定義字段及后臺編輯功能區(qū)域
這篇文章主要介紹了WordPress中給文章添加自定義字段及后臺編輯區(qū)域的相關(guān)函數(shù),分別簡單講了add_post_meta和add_meta_box 的用法,需要的朋友可以參考下2015-12-12