當前位置:首頁 » 編程語言 » php獲取天數

php獲取天數

發布時間: 2022-07-18 07:22:55

『壹』 php獲取當月天數及當月第一天及最後一天、上月第一天及最後一天實現方法是什麼

date('Y-m-01', strtotime('-1 month')); //上個月第一天
date('Y-m-t', strtotime('-1 month')); //上個月最後一天
$BeginDate=date('Y-m-01', strtotime(date("Y-m-d"))); //當月第一天
date('Y-m-d', strtotime("$BeginDate +1 month -1 day")); //當月最後一天

『貳』 php根據年月獲取當月天數及日期數組的方法

本文實例講述了php根據年月獲取當月天數及日期數組的方法。分享給大家供大家參考,具體如下:
function
get_day(
$date
)
{
$tem
=
explode('-'
,
$date);
//切割日期
得到年份和月份
$year
=
$tem['0'];
$month
=
$tem['1'];
if(
in_array($month
,
array(
1
,
3
,
5
,
7
,
8
,
01
,
03
,
05
,
07
,
08
,
10
,
12)))
{
//
$text
=
$year.'年的'.$month.'月有31天';
$text
=
'31';
}
elseif(
$month
==
2
)
{
if
(
$year%400
==
0
||
($year%4
==
0
&&
$year%100
!==
0)
)
//判斷是否是閏年
{
//
$text
=
$year.'年的'.$month.'月有29天';
$text
=
'29';
}
else{
//
$text
=
$year.'年的'.$month.'月有28天';
$text
=
'28';
}
}
else{
//
$text
=
$year.'年的'.$month.'月有30天';
$text
=
'30';
}
return
$text;
}
echo
get_day('2016-8-1');
運行結果為:31
改造,返回日期數組:
/**
*
獲取當月天數
*
@param
$date
*
@param
$rtype
1天數
2具體日期數組
*
@return
*/
function
get_day(
$date
,$rtype
=
'1')
{
$tem
=
explode('-'
,
$date);
//切割日期
得到年份和月份
$year
=
$tem['0'];
$month
=
$tem['1'];
if(
in_array($month
,
array(
1
,
3
,
5
,
7
,
8
,
01
,
03
,
05
,
07
,
08
,
10
,
12)))
{
//
$text
=
$year.'年的'.$month.'月有31天';
$text
=
'31';
}
elseif(
$month
==
2
)
{
if
(
$year%400
==
0
||
($year%4
==
0
&&
$year%100
!==
0)
)
//判斷是否是閏年
{
//
$text
=
$year.'年的'.$month.'月有29天';
$text
=
'29';
}
else{
//
$text
=
$year.'年的'.$month.'月有28天';
$text
=
'28';
}
}
else{
//
$text
=
$year.'年的'.$month.'月有30天';
$text
=
'30';
}
if
($rtype
==
'2')
{
for
($i
=
1;
$i
<=
$text
;
$i
++
)
{
$r[]
=
$year."-".$month."-".$i;
}
}
else
{
$r
=
$text;
}
return
$r;
}
var_mp(get_day('2016-8-1','2'));
運行結果如下:
array(31)
{
[0]=>
string(8)
"2016-8-1"
[1]=>
string(8)
"2016-8-2"
[2]=>
string(8)
"2016-8-3"
[3]=>
string(8)
"2016-8-4"
[4]=>
string(8)
"2016-8-5"
[5]=>
string(8)
"2016-8-6"
[6]=>
string(8)
"2016-8-7"
[7]=>
string(8)
"2016-8-8"
[8]=>
string(8)
"2016-8-9"
[9]=>
string(9)
"2016-8-10"
[10]=>
string(9)
"2016-8-11"
[11]=>
string(9)
"2016-8-12"
[12]=>
string(9)
"2016-8-13"
[13]=>
string(9)
"2016-8-14"
[14]=>
string(9)
"2016-8-15"
[15]=>
string(9)
"2016-8-16"
[16]=>
string(9)
"2016-8-17"
[17]=>
string(9)
"2016-8-18"
[18]=>
string(9)
"2016-8-19"
[19]=>
string(9)
"2016-8-20"
[20]=>
string(9)
"2016-8-21"
[21]=>
string(9)
"2016-8-22"
[22]=>
string(9)
"2016-8-23"
[23]=>
string(9)
"2016-8-24"
[24]=>
string(9)
"2016-8-25"
[25]=>
string(9)
"2016-8-26"
[26]=>
string(9)
"2016-8-27"
[27]=>
string(9)
"2016-8-28"
[28]=>
string(9)
"2016-8-29"
[29]=>
string(9)
"2016-8-30"
[30]=>
string(9)
"2016-8-31"
}
更多關於PHP相關內容感興趣的讀者可查看本站專題:《php日期與時間用法總結》、《PHP數組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php面向對象程序設計入門教程》、《PHP網路編程技巧總結》、《php字元串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。

『叄』 在php怎麼得到當月的總天數

<?php
$days = cal_days_in_month(CAL_GREGORIAN, 4, 2011);
echo "返回2011-4的天數 ".$days."<br/>";
$days = date('t', strtotime("2011-4-1"));
echo "返回2011-4的天數 ".$days."<br/>";
$days = date("t");
echo "當前月的天數 ".$days."<br/>";
?>

『肆』 php 計算天數

我不知道你所說的資料庫日期是什麼意思

我個你一個計算天數的代碼,你自己更改裡面的變數

$days = abs(strtotime($db_time) - strtotime(date("Y-m-d")))/86400;

裡面的$db_time就是你指的資料庫的日期,我不知道你指什麼,幫不了你寫,你只需要更改$db_time為你要計算的日期就可以

$days就是相隔的天數.

『伍』 php 求每個月所有天數的方法

<?php
$days=date('t');
echodate('m').'月有'.$days."天<br>";

for($i=1;$i<=$days;$i++){
echodate('Y-m-').$i."<br>";
}

『陸』 php計算兩個時間之間的天數

<?php
$days=cal_days_in_month(CAL_GREGORIAN,4,2011);
echo"返回2011-4的天數".$days."<br/>";
$days=date('t',strtotime("2011-4-1"));
echo"返回2011-4的天數".$days."<br/>";
$days=date("t");
echo"當前月的天數".$days."<br/>";

$thisday=date("d",time());


//循環當前天數到當前月底日期
for($i=$thisday;$i<=$days;$i++){
//在這里進行循環,如果跨幾個月的話,就外層再加一個循環月份的就可以了
}
?>

『柒』 php 算出從今天到周日的天數

date_default_timezone_set("Etc/GMT-8");
$nowtime=date("Y-m-dH:i",time());
$w=date("w",time());
if($w==0)
$w="今天是星期天!";
else
{
$day=7-$w;
$w="今天離星期天還有$day天";
}
echo"當前時間為:".$nowtime;
echo"</br>".$w;

『捌』 php演算法編程 計算天數

<?php
$start='2017-2-5';
$end='2018-3-6';
functioncalDate($start,$end){
$start_time=strtotime($start); //獲得$start的秒時間戳
$end_time=strtotime($end); //獲得$end的秒時間戳
return($end_time-$start_time)/60/60/24; //秒時間戳相減得到兩個日期相差的秒數,通過秒數計算天數
}
echocalDate($start,$end);
?>

『玖』 php寫出一個函數,參數為年份和月份,輸出結果為指定月的天數

<?php
functiongetDays($date){
//獲取天數
$days=date("t",strtotime($date));
return$days;
}

//2015年12月
$date="2015-12";
echogetDays($date);
//輸出結果:31天

?>

熱點內容
源神比較好的雲伺服器 發布:2025-02-01 13:55:27 瀏覽:206
黑蘋果idea編譯慢 發布:2025-02-01 13:45:30 瀏覽:548
c和linux 發布:2025-02-01 13:39:38 瀏覽:175
android實現列表 發布:2025-02-01 13:38:06 瀏覽:102
安卓手機為什麼會出現橫條 發布:2025-02-01 13:33:38 瀏覽:625
jqueryajax提交文件上傳 發布:2025-02-01 13:32:03 瀏覽:689
沒連上wifi如何修改wifi密碼 發布:2025-02-01 13:20:42 瀏覽:758
絕對賽車3安卓在哪裡下載 發布:2025-02-01 12:42:30 瀏覽:718
mysql修改資料庫字元 發布:2025-02-01 12:37:52 瀏覽:570
阿里雲伺服器出廠密碼是多少 發布:2025-02-01 12:35:43 瀏覽:668