1、获取某日期后一周、一月、一年的日期 php
//获取某日期后三周同一天日期
public static function getNextDate($date){
$return = [
date( 'Y-m-d', strtotime("$date +1 day") ),
date( 'Y-m-d', strtotime("$date +1 week") ),
date( 'Y-m-d', strtotime("$date +1 month") ),
date( 'Y-m-d', strtotime("$date +1 year") ),
];
return $return;
}
//日期之前用- $date = '2020-10-11';
2、php获取两个日期相差多少个月份
function getMonthNum($date1,$data2){
$data1_time = strtotime($date1);
$data2_time = strtotime($data2);
$Y = date('Y',$data2_time) - date('Y',$data1_time);
$M = date('m',$data2_time) - date('m',$data1_time);
return $res = abs($Y*12 + $M);
}
$months=getMonthNum('2019-12-11','2019-10-01');
echo $months;
参考网址:https://blog.youkuaiyun.com/loveyoulouyou/article/details/103405357
3、一周的开始时间和结束时间
$week 两位数
//当周起止时间戳
$timestamp['start'] = strtotime($year.'W'.$week);
$timestamp['end'] = strtotime('+1 week -1 day',$timestamp['start']);
//当周起止日期
$timeymd['start'] = date("Y-m-d",$timestamp['start']);
$timeymd['end'] = date("Y-m-d",$timestamp['end']); //返回起始时间戳
4、PHP数字补零的两种方法
在php中有两个函数——至少有两个是否有其他的我还不知道,能够实现数字补零,str_pad(),sprintf()详细如下
str_pad
顾名思义这个函数是针对字符串来说的这个可以对指定的字符串填补任何其它的字符串
例如:str_pad(带填补的字符串,填补后的长度,填补字符串,填补位置)
其中填补后的长度必须是个正整数,填补位置有三个选项,
左边:STR_PAD_LEFT,
右边:STR_PAD_RIGHT,
两端:STR_PAD_BOTH
例如:
1 |
|
结果:00000001
1 |
|
结果:10000000
1 |
|
结果:00010000
在上边的例子中值得注意的一个细节是,如果填补的位数是个奇数,例如例三中填补了7个0,右边优先。
再看补零的另外一种方法sprintf
这个函数学过c的都十分了解它,呵呵……
不过咱不说这么多,因为用起来实在太灵活了,以至于我基本不会用,不过在左边补零(或者在小数点后补零)用起来还是很方便的
先看左边补零
1 |
|
先说%05d的意思,用一个5位数的数字格式化后边的参数,如果不足5位就补零
运行结果是00005
再看小数点后补零
1 |
|
%01.3f的意思是说,用一个小数点后最少三位不足三位补零,小数点前最少一位,不足一位补零的浮点数格式化后边的参数
其运行结果是:1.000
参考网址:https://www.cnblogs.com/52php/p/5657892.html
前一天的日期为:
date("Y-m-d",strtotime("-1 days",strtotime('2019-08-31')))
前一月的日期为
date("Y-m-d",strtotime("-1 months",strtotime('2019-08-31')))
前一年的日期为:
date("Y-m-d",strtotime("-1 years",strtotime('2019-08-31')))
后一天的日期为:
date("Y-m-d",strtotime("+1 days",strtotime('2019-08-31')))
后一月的日期为:
date("Y-m-d",strtotime("+1 months",strtotime('2019-08-31')))
后一年的日期为:
date("Y-m-d",strtotime("+1 years",strtotime('2019-08-31')))
6、php 获取一个月的开始及结束时间戳
假设已知当前的时间格式为 :$time = '2018-05';
$timebegin = strtotime($time) ; //开始时间戳
$day = date('t',$timebegin);
$timeend = $timebegin + 86400 * $day - 1; //结束时间戳
原文链接:https://blog.youkuaiyun.com/qq_39646453/article/details/80700991
7、php获取一年有多少天
function cal_days_in_year($year){
$days = 0;
for($month=1;$month<=12;$month++){
$days = $days + cal_days_in_month(CAL_GREGORIAN,$month,$year);
}
return $days;
}