导航:首页 > 编程语言 > php取当月第一天

php取当月第一天

发布时间:2022-05-10 18:38:41

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

阅读全文

与php取当月第一天相关的资料

热点内容
肯尼亚程序员 浏览:638
新科源码 浏览:659
如何判断服务器有没有带宽 浏览:41
天正建筑批量删除命令 浏览:94
cad最下面的一排命令都什么意思 浏览:456
pythonimportcpp 浏览:850
W10的系统怎么给U盘加密 浏览:370
华为手机代码编程教学入门 浏览:762
和彩云没会员怎样解压 浏览:634
androidimageview保存 浏览:387
新买店铺什么服务器 浏览:883
文件夹能直接刻录吗 浏览:493
androidxmpp删除好友 浏览:969
javac哪个前景好 浏览:428
中华英才网app为什么不能搜索了 浏览:660
服务器域名是什么意思 浏览:52
Linux导出mysql命令 浏览:159
无诈建邺是什么app 浏览:228
python中的双色球 浏览:168
python解释器里如何换行 浏览:413