有時(shí)候我們?cè)陂_(kāi)發(fā)項(xiàng)目的時(shí)候會(huì)遇到獲取幾天前、幾天后、今日、本周等的開(kāi)始時(shí)間和結(jié)束時(shí)間,獲取前幾個(gè)月份,獲取本年度天數(shù)等等,以下是我總結(jié)的幾種常見(jiàn)的時(shí)間相關(guān)的方法,希望對(duì)大家有用:
1.獲取前幾個(gè)月月份
public ?function toSelfMonth($m){
? ?$today = input('param.today') ? input('param.today') : date("Y-m-d");
? ?$arr = array();
? ?$old_time = strtotime('-'.$m.' month',strtotime($today));
? ?for($i = 0;$i <= $m; ++$i){
$t = strtotime("+$i month",$old_time);
$arr[]=date('Y-m',$t);
}
return $arr;
}
2.獲取前幾天日期
public function toSelfDay($m){
$today = input('param.today') ? input('param.today') : date("Y-m-d");
$arr = array();
$old_time = strtotime('-'.$m.' day',strtotime($today));
for($i = 0;$i <= $m; ++$i){
$t = strtotime("+$i day",$old_time);
$arr[]=date('Y-m-d',$t);
}
return $arr;
}
3.計(jì)算前幾個(gè)月的天數(shù)
public function getLastMonthDays($month){
$arr = $this->toSelfMonth($month);
? ?$y = date('Y',time());
? ?$days = date('d',time());
? ?for ($i=0;$i
}
return $days;
}
4.獲取某一月的天數(shù)
public function getSelfMonthDays(){
/*$days = cal_days_in_month(CAL_GREGORIAN, 4, 2011);*/
$days = date('t', strtotime("2011-4-1"));
/*$days = date("t");
echo "當(dāng)前月的天數(shù) ".$days."
";*/
? ?return $days;
}
5.獲取指定年月的開(kāi)始和結(jié)束時(shí)間戳
public function getSelfMonthStartEnd($y=0,$m=0){
? ?$y = $y ? $y : date('Y');
? ?$m = $m ? $m : date('m');
? ?$d = date('t', strtotime($y.'-'.$m));
? ?return array("start"=>strtotime($y.'-'.$m),"end"=>mktime(23,59,59,$m,$d,$y));
}
6.獲取日期間隔
獲取時(shí)間間隔,首先要將時(shí)間轉(zhuǎn)化為時(shí)間戳,時(shí)間戳一般是以秒為計(jì)量單位,所以得出以下計(jì)量單位:
(1)1小時(shí)為3600s
(2)1天為24*3600s,即86400秒
根據(jù)這兩個(gè)條件,得到以下方法
function GetDateLong($time1,$time2){
? ?if($time2 >= $time1){
? ? ? ?$long = $time2 - $time1;
? ?}else{
? ? ? ?$long = -($time2 - $time1);
? ?}
? ?if($long > 0){
? ? ? ?$year_day = $this->GetYearDay($time1);
? ? ? ?$year = floor($long/($year_day*86400));
? ? ? ?$day = floor(($long)%($year_day*86400)/86400);
? ? ? ?if($year >= 1){
? ? ? ? ? ?return $year.'年'.$day.'天';
? ? ? ?}else{
? ? ? ? ? ?return $day.'天';
? ? ? ?}
? ?}else{
? ? ? ?return '0天';
? ?}
}
7.獲取是否是閏年
一年有365天的情況下是平年,1年有366天的情況下是閏年。
1、普通閏年:公歷年份是4的倍數(shù)的,一般是閏年。(如2004年就是閏年);2、世紀(jì)閏年:公歷年份是整百數(shù)的,必須是400的倍數(shù)才是閏年(如1900年不是世紀(jì)閏年,2000年是世紀(jì)閏年)。
按照這個(gè)條件我們可以獲取到以下的方法獲取是平年還是閏年
public function GetYearType($year){
? ?if ($year%4==0&&($year%100!=0 || $year%400==0)){
? ? ? ?return 1;
? ?}else{
? ? ? ?return 2;
? ?}
}
8.判斷一年有多少天
同以上7的介紹公歷年份是4的倍數(shù)的,一般是閏年。公歷年份是整百數(shù)的,必須是400的倍數(shù)才是閏年。
按照這個(gè)條件我們可以完成一下方法來(lái)獲取一年的天數(shù)
public function GetYearDay($time){
? ?$year = date("Y",$time);
? ?$a1 = $year/100;
? ?if($a1 == 0){
? ? ? ?$b1 = $year/400;
? ? ? ?if($b1 > 0){
? ? ? ? ? ?$long = 365;
? ? ? ?}else{
? ? ? ? ? ?$long = 366;
? ? ? ?}
? ?}else{
? ? ? ?$b2 = $year/4;
? ? ? ?if($b2 == 0){
? ? ? ? ? ?$long = 366;
? ? ? ?}else{
? ? ? ? ? ?$long = 365;
? ? ? ?}
? ?}
? ?return $long;
}
以上是PHP幾種常見(jiàn)的時(shí)間間隔信息獲取的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費(fèi)脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機(jī)

Video Face Swap
使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱門(mén)文章

熱工具

記事本++7.3.1
好用且免費(fèi)的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強(qiáng)大的PHP集成開(kāi)發(fā)環(huán)境

Dreamweaver CS6
視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版
神級(jí)代碼編輯軟件(SublimeText3)