導航:首頁 > 編程語言 > php獲取當前月份天數

php獲取當前月份天數

發布時間:2022-04-20 16:21:16

1. 用php代碼如何計算今天是今年的第多少天速度急急急

樓上的

date("z")

或者:

$d = getdate();$d["yday"];

確實直接得到天數,如果要自己算的話,可以如下:

每一年的開始都是1月1日,所以用今天的時間戳減去一月一日的時間戳,再除以86400(都是凌晨的時間戳,所以肯定是86400的倍數),就是天數

<?php
$today=strtotime(date('Ymd'));
$year_start=strtotime(date(Y0101));

$days=($today-$year_start)/86400+1;//考慮到1月1日是第一天,所以+1

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

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

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

?>

3. 在php怎麼得到當月的總天數

echo " 本月共有:".date("t")."天";

4. 請問哪位高手,如何用php獲取當前日期的7天內的時間,比如今天是5號,我想獲取5號前7天的日期,怎麼獲取

$t = time()+3600*8;//這里和標准時間相差8小時需要補足
$tget = $t-3600*24*5;//比如5天前的時間
echo date("Y-m-d H:i:s 星期w",$tget);//格式按你需要選取

附帶:
相關時間參數:
a - "am" 或是 "pm"
A - "AM" 或是 "PM"
d - 幾日,二位數字,若不足二位則前面補零; 如: "01" 至 "31"
D - 星期幾,三個英文字母; 如: "Fri"
F - 月份,英文全名; 如: "January"
h - 12 小時制的小時; 如: "01" 至 "12"
H - 24 小時制的小時; 如: "00" 至 "23"
g - 12 小時制的小時,不足二位不補零; 如: "1" 至 12"
G - 24 小時制的小時,不足二位不補零; 如: "0" 至 "23"
i - 分鍾; 如: "00" 至 "59"
j - 幾日,二位數字,若不足二位不補零; 如: "1" 至 "31"
l - 星期幾,英文全名; 如: "Friday"
m - 月份,二位數字,若不足二位則在前面補零; 如: "01" 至 "12"
n - 月份,二位數字,若不足二位則不補零; 如: "1" 至 "12"
M - 月份,三個英文字母; 如: "Jan"
s - 秒; 如: "00" 至 "59"
S - 字尾加英文序數,二個英文字母; 如: "th","nd"
t - 指定月份的天數; 如: "28" 至 "31"
U - 總秒數
w - 數字型的星期幾,如: "0" (星期日) 至 "6" (星期六)
Y - 年,四位數字; 如: "1999"
y - 年,二位數字; 如: "99"
z - 一年中的第幾天; 如: "0" 至 "365"

5. PHP下獲取上個月、下個月、本月的日期(strtotime,date)

今天寫程序的時候,突然發現了很早以前寫的獲取月份天數的函數,經典的switch版,但是獲得上月天數的時候,我只是把月份-1了,估計當時太困了吧,再看到有種毛骨悚然的感覺,本來是想再處理一下的,但是一想肯定還有什麼超方便的方法,於是找到了下面這個版本,做了一點小修改。
獲取本月日期:
復制代碼
代碼如下:
function
getMonth($date){

$firstday
=
date("Y-m-01",strtotime($date));

$lastday
=
date("Y-m-d",strtotime("$firstday
+1
month
-1
day"));

return
array($firstday,$lastday);
}
$firstday是月份的第一天,假如$date是2014-2這樣的話,$firstday就會是2014-02-01,然後根據$firstday加一個月就是2014-03-01,再減一天就是2014-02-28,用date()和strtotime()真是太方便了。
獲取上月日期:
復制代碼
代碼如下:
function
getlastMonthDays($date){

$timestamp=strtotime($date);

$firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));

$lastday=date('Y-m-d',strtotime("$firstday
+1
month
-1
day"));

return
array($firstday,$lastday);
}
上月日期需要先獲取一個時間戳,然後在月份上-1就OK了,超智能的date()會把2014-0-1這種東西轉換成2013-12-01,太爽了。
獲取下月日期:
復制代碼
代碼如下:
function
getNextMonthDays($date){

$timestamp=strtotime($date);

$arr=getdate($timestamp);

if($arr['mon']
==
12){

$year=$arr['year']
+1;

$month=$arr['mon']
-11;

$firstday=$year.'-0'.$month.'-01';

$lastday=date('Y-m-d',strtotime("$firstday
+1
month
-1
day"));

}else{

$firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)+1).'-01'));

$lastday=date('Y-m-d',strtotime("$firstday
+1
month
-1
day"));

}

return
array($firstday,$lastday);
}
下月日期的代碼看起來比較長一點,因為date()轉不了類似2014-13-01這種東西,它會直接回到1970,所以前面需要處理一下12月的問題,除了12月就直接月份+1就OK啦。
總得來說,還是很方便的,日期函數太強大了。

6. 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")); //當月最後一天

7. php 求每個月所有天數的方法

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

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

8. 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程序設計有所幫助。

9. 使用PHP如何獲取某個月的所有日期

$j = date("t"); //獲取當前月份天數
$start_time = strtotime(date('Y-m-01')); //獲取本月第一天時間戳
$array = array();
for($i=0;$i<$j;$i++){
$array[] = date('Y-m-d',$start_time+$i*86400); //每隔一天賦值給數組
}

print_r($array);

閱讀全文

與php獲取當前月份天數相關的資料

熱點內容
c523壓縮比 瀏覽:543
命令語氣的人什麼心態 瀏覽:435
程序員喜歡留指甲嗎 瀏覽:516
七牛雲伺服器收費標准 瀏覽:627
時光相冊加密空間密碼忘記 瀏覽:474
華為雲為用戶提供的服務雲伺服器 瀏覽:634
minecraftlinux伺服器搭建 瀏覽:376
linux命令新建文件 瀏覽:708
長線pdf 瀏覽:607
程序員電腦支持手寫 瀏覽:414
解壓頭戴式耳機推薦 瀏覽:344
紙條app上怎麼樣看對方主頁 瀏覽:883
編譯英語單詞怎麼寫 瀏覽:249
編譯原理和匯編原理的區別 瀏覽:864
如何給加密的pdf解密 瀏覽:770
華為盒子時間同步伺服器地址 瀏覽:95
python處理excel亂碼 瀏覽:391
mysql的命令行 瀏覽:822
jpeg採用什麼演算法 瀏覽:701
程序員紅軸薄膜 瀏覽:306