当前位置:首页 » 编程语言 » php获取天数

php获取天数

发布时间: 2022-07-18 07:22:55

‘壹’ 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")); //当月最后一天

‘贰’ 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程序设计有所帮助。

‘叁’ 在php怎么得到当月的总天数

<?php
$days = cal_days_in_month(CAL_GREGORIAN, 4, 2011);
echo "返回2011-4的天数 ".$days."<br/>";
$days = date('t', strtotime("2011-4-1"));
echo "返回2011-4的天数 ".$days."<br/>";
$days = date("t");
echo "当前月的天数 ".$days."<br/>";
?>

‘肆’ php 计算天数

我不知道你所说的数据库日期是什么意思

我个你一个计算天数的代码,你自己更改里面的变量

$days = abs(strtotime($db_time) - strtotime(date("Y-m-d")))/86400;

里面的$db_time就是你指的数据库的日期,我不知道你指什么,帮不了你写,你只需要更改$db_time为你要计算的日期就可以

$days就是相隔的天数.

‘伍’ php 求每个月所有天数的方法

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

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

‘陆’ php计算两个时间之间的天数

<?php
$days=cal_days_in_month(CAL_GREGORIAN,4,2011);
echo"返回2011-4的天数".$days."<br/>";
$days=date('t',strtotime("2011-4-1"));
echo"返回2011-4的天数".$days."<br/>";
$days=date("t");
echo"当前月的天数".$days."<br/>";

$thisday=date("d",time());


//循环当前天数到当前月底日期
for($i=$thisday;$i<=$days;$i++){
//在这里进行循环,如果跨几个月的话,就外层再加一个循环月份的就可以了
}
?>

‘柒’ php 算出从今天到周日的天数

date_default_timezone_set("Etc/GMT-8");
$nowtime=date("Y-m-dH:i",time());
$w=date("w",time());
if($w==0)
$w="今天是星期天!";
else
{
$day=7-$w;
$w="今天离星期天还有$day天";
}
echo"当前时间为:".$nowtime;
echo"</br>".$w;

‘捌’ php算法编程 计算天数

<?php
$start='2017-2-5';
$end='2018-3-6';
functioncalDate($start,$end){
$start_time=strtotime($start); //获得$start的秒时间戳
$end_time=strtotime($end); //获得$end的秒时间戳
return($end_time-$start_time)/60/60/24; //秒时间戳相减得到两个日期相差的秒数,通过秒数计算天数
}
echocalDate($start,$end);
?>

‘玖’ php写出一个函数,参数为年份和月份,输出结果为指定月的天数

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

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

?>

热点内容
android亮度获取 发布:2025-02-01 12:09:10 浏览:624
小孩什么时候学编程比较好 发布:2025-02-01 12:03:10 浏览:960
c语言的认识 发布:2025-02-01 11:58:03 浏览:520
svn连接服务器地址 发布:2025-02-01 11:51:31 浏览:416
对源程序为什么要编译 发布:2025-02-01 11:47:46 浏览:218
sql表添加记录 发布:2025-02-01 11:22:08 浏览:864
word编辑加密 发布:2025-02-01 11:18:53 浏览:571
php变量文本 发布:2025-02-01 11:10:46 浏览:426
音悦台上传mv 发布:2025-02-01 11:05:02 浏览:516
微信如何设置访问限制 发布:2025-02-01 10:43:06 浏览:335