1. 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")); //當月最後一天
2. 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程序設計有所幫助。
3. PHP DATE 如何取得當月的第一天和最後一天
本文實例講述了PHP獲取指定月份第一天和最後一天的方法。分享給大家供大家參考。具體如下:
復制代碼 代碼如下:$date = date(time());
$start_date = date('Y-m-d', mktime(00, 00, 00, date('m', strtotime($date))+1, 01));
$end_date = date('Y-m-d', mktime(23, 59, 59, date('m', strtotime($date))+2, 00));
希望本文所述對大家的php程序設計有所幫助。
4. 怎麼用PHP獲得當前月的1號是從星期幾開始的呢急急急!!!!!
$xingqi = Date("w", strtotime(Date("Y-n-1"))) ;
switch($xingqi)
{
case 0: $xingqi = "星期日";
break;
case 1: $xingqi = "星期一";
break;
case 2: $xingqi = "星期二";
break;
case 3: $xingqi = "星期三";
break;
case 4: $xingqi = "星期四";
break;
case 5: $xingqi = "星期五";
break;
case 6: $xingqi = "星期六";
break;
default: echo "獲取不到星期,有錯誤!";
}
echo "本月的第一天是: ".$xingqi;
5. php獲取當年的每個月的第一天和最後一天時間戳的函數
/**
* 獲取指定月份的第一天開始和最後一天結束的時間戳
*
* @param int $y 年份 $m 月份
* @return array(本月開始時間,本月結束時間)
*/
function mFristAndLast($y="",$m=""){
if($y=="") $y=date("Y");
if($m=="") $m=date("m");
$m=sprintf("%02d",intval($m));
$y=str_pad(intval($y),4,"0",STR_PAD_RIGHT);
$m>12||$m<1?$m=1:$m=$m;
$firstday=strtotime($y.$m."01000000");
$firstdaystr=date("Y-m-01",$firstday);
$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
return array("firstday"=>$firstday,"lastday"=>$lastday);
}
6. php求當前季度的第一天和最後一天
$date = getdate();
$month = $date['mon']; //當前第幾個月
$year = $date['year']; //但前的年份
$strart = floor($month/3) * 3; //單季第一個月
$strart = mktime(0,0,0,$start,1,$year); //當季第一天的時間戳
$end = mktime(0,0,0,$start+3,1,$year); //當季最後一天的時間戳
7. php有沒相關函數可以獲取給定日期的上個月的第一天及最後一天
沒有,可以取當前月的第一天00:00的時間戳,然後減24小時,再date即是上個月的最後一天。
如:
$prev_month_last_day = date("Y/m/d", strtotime(date("Y-m-1")) - 86400);
list($year, $month, $day) = explode("/", $prev_month_last_day);
$prev_month_first_day = $year . '/' . $month . '/1';
printf("first day of prev month is : %s\nlast day of prev month is : %s\n", $prev_month_first_day, $prev_month_last_day);
8. php中如何獲取最近六個月每個月的起始時間和結束時間
你要實現的是不是當前月份和當前月份往前5個月,每個月的第一天是幾號號最後一天是幾號?如果是的話,我寫了一個 能實現你的需求。你的問題讓我好糾結。
$currentTime=time();
$cyear=floor(date("Y",$currentTime));
$cMonth=floor(date("m",$currentTime));
for($i=0;$i<6;$i++){
$nMonth=$cMonth-$i;
$cyear=$nMonth==0?($cyear-1):$cyear;
$nMonth=$nMonth<=0?12+$nMonth:$nMonth;
$date=$cyear."-".$nMonth."-1";
$firstday=date('Y-m-01',strtotime($date));
$lastday=date('Y-m-t',strtotime($date));
echo$cyear."年".$nMonth."月";
echo"第一天:".$firstday;
echo"最後一天:".$lastday,"<br>";
}
9. php中用time()函數存入時間,如何查詢當月的數據
這個time()函數是將時間保存成時間戳格式,則要查當月數據,只要查當月第一天到當月最後一天的之間的數據即可。
假設這個用來判斷的欄位是date
sql語句
SELECT ………… WHERE………… `date` >= 本月第一天的time值 AND `date` < 下個月第一天的time值
所以這里就只要獲取當月第一天以及下個月第一天的時間戳
具體如下:
<?php
$cur = date('Y-m',time());//當天年月
$cur_y = date('Y',time());//當天年份
$cur_m = date('m',time());//當天月份
$cur_f = $cur . '-1';//本月首日
$first = strtotime($cur_f);//時間戳最小值,本月第一天時間戳
//下月首日
if($cur_m>=12){
$cur_n = ($cur_y+1) . '-1-1';
}else{
$cur_n = $cur_y . '-' . ($cur_m+1) . '-1';
}
$last = strtotime($cur_n);//時間戳最大值,下個月第一天時間戳
?>
再把$first 和 $last 放入sql語句裡面就可以查詢到數據了
10. 本年的第一天和最後一天,用PHP怎麼得到
$BeginDate=date('Y-m-01', strtotime(date("Y-m-d"))); 獲取當前月份第一天
echo $BeginDate;
echo date('Y-m-d', strtotime("$BeginDate +1 month -1 day")); 加一個月減去一天。
-----------------------------------
function getthemonth($date)
{
$firstday = date('Y-m-01', strtotime($date));
$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
return array($firstday, $lastday);
}