php獲取月份
A. 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")); //當月最後一天
B. 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啦。
總得來說,還是很方便的,日期函數太強大了。
C. php 獲取月份數據怎麼寫
取出時間戳,然後利用PHP格式化時間戳就可以看到月了。date('m',這里是從庫中取出的時間戳);這樣輸出出來的就是月了!
D. PHP獲取當前日期所在星期(月份)的開始日期與結束日期(實現代碼)
代碼如下:
// 獲取指定日期所在星期的開始時間與結束時間
function getWeekRange($date){
$ret=array();
$timestamp=strtotime($date);
$w=strftime('%u',$timestamp);
$ret['sdate']=date('Y-m-d 00:00:00',$timestamp-($w-1)*86400);
$ret['edate']=date('Y-m-d 23:59:59',$timestamp+(7-$w)*86400);
return $ret;
}
// 獲取指定日期所在月的開始日期與結束日期
function getMonthRange($date){
$ret=array();
$timestamp=strtotime($date);
$mdays=date('t',$timestamp);
$ret['sdate']=date('Y-m-1 00:00:00',$timestamp);
$ret['edate']=date('Y-m-'.$mdays.' 23:59:59',$timestamp);
return $ret;
}
// 以上兩個函數的應用
function getFilter($n){
$ret=array();
switch($n){
case 1:// 昨天
$ret['sdate']=date('Y-m-d 00:00:00',strtotime('-1 day'));
$ret['edate']=date('Y-m-d 23:59:59',strtotime('-1 day'));
break;
case 2://本星期
$ret=getWeekRange(date('Y-m-d'));
break;
case 3://上一個星期
$strDate=date('Y-m-d',strtotime('-1 week'));
$ret=getWeekRange($strDate);
break;
case 4: //上上星期
$strDate=date('Y-m-d',strtotime('-2 week'));
$ret=getWeekRange($strDate);
break;
case 5: //本月
$ret=getMonthRange(date('Y-m-d'));
break;
case 6://上月
$strDate=date('Y-m-d',strtotime('-1 month'));
$ret=getMonthRange($strDate);
break;
}
return $ret;
}
E. PHP如何查詢連續月份(跨年)
/**
* 獲取指定日期之間的各個月
*/
public function get_months($sdate, $edate) {
$range_arr = array();
do {
$monthinfo = $this->get_monthinfo_by_date($sdate);
$end_day = $monthinfo['month_end_day'];
$start = $this->substr_date($monthinfo['month_start_day']);
$end = $this->substr_date($monthinfo['month_end_day']);
$range = "{$start} ~ {$end}";
$range_arr[] = $range;
$sdate = date('Y-m-d', strtotime($sdate.'+1 month'));
}while($end_day < $edate);
return $range_arr;
}
/**
* 截取日期中的月份和日
* @param string $date
* @return string $date
*/
public function substr_date($date) {
if ( ! $date) return FALSE;
return date('m-d', strtotime($date));
}
/**
* 檢查日期的有效性 YYYY-mm-dd
* @param array $date_arr
* @return boolean
*/
public function check_date($date_arr) {
$invalid_date_arr = array();
foreach ($date_arr as $row) {
$timestamp = strtotime($row);
$standard = date('Y-m-d', $timestamp);
if ($standard != $row) $invalid_date_arr[] = $row;
}
if ( ! empty($invalid_date_arr)) {
die("invalid date -> ".print_r($invalid_date_arr, TRUE));
}
}
F. php 如何用date取得指定月份有多少天
$time = strtotime("2013-02-01");
echo date('t', $time);
你思路是對的,只要把日期格式補完就可以了。
G. php怎樣獲取日期中的月份
示例代碼如下:
<?php
//日期
$date="2016-11-1111:11:11";
//轉換成時間戳
$timestrap=strtotime($date);
//格式化,取出月份
echodate('m',$timestrap);
H. PHP 如何獲取兩個時間之間的年和月份
$time1 = strtotime('2014-02-04'); // 自動為00:00:00 時分秒
$time2 = strtotime('2015-02-06');
$monarr = array();
$monarr[] = '2014-02'; // 當前月;
while( ($time1 = strtotime('+1 month', $time1)) <= $time2){
$monarr[] = date('Y-m',$time1); // 取得遞增月;
}
print_r($monarr);
I. php如何讀取月份及日期
include("conn.php");
$a="select*fromqiandanorderbyid";
$res=mysql_query($a,$conn);
$r=mysql_fetch_array($res);
$date=$r['date'];
echo$t;
date('m',strtotime('2011-08-25'));
echo$yue;
J. php 怎麼 獲取 整年月份
使用函式 date() 實現
第一種:簡單(供學慣用)
<?PHP
$today=date("Y-m-dG:i:s");
echo"<center>$today</center>";
?>
第二種:
/*
FormatTime
*/
FunctionformatTime($time,$type="1"){
switch($type){
case1;#2002-06-0418:58Tuesday
returndate("Y.m.dH:i",$time)."<fontcolor=blue>".date("l",$time)."</font>";
case2;#June2002
returndate("MY",$time);
case3;#2002-06-0418:58
returndate("Y.m.dH:i",$time);
case4;#06-04AM
returndate("m-dA",$time);
case5;#06-0418:58
returndate("m.dH:i",$time);
}
}