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);
}
}