PHP日期与时间戳

1、当前时间,返回的是时间戳格式:time()
在这里插入图片描述
2、时间戳格式转化成日期格式:date(‘Y-m-d h:i:s’,你的时间戳),前一个日期时间格式可以选择不同的形式,可以不用年份或者不用时分秒之类的
在这里插入图片描述
在这里插入图片描述
3、带上午am、下午pm标识的日期:date(‘Y-m-d h:i:sa’,time())
在这里插入图片描述
4、从本周开始至当前日期——时间戳格式:
在这里插入图片描述

$begin = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));
$end = time();

5、表示上周7天时间段——时间戳格式:

$begin=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
$end=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));

6、表示上个月整月时间段——时间戳格式:

$begin = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
$end = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d').'day')));

7、表示本月整个月的时间段——时间戳格式:

$begin=mktime(0,0,0,date('m'),1,date('Y'));
$end=mktime(23,59,59,date('m'),date('t'),date('Y'));

8、表示本年开始到本年结束——时间戳格式:

$begin_year = strtotime(date("Y",time())."-1"."-1"); //本年开始
$end_year = strtotime(date("Y",time())."-12"."-31"); //本年结束
//5-8的代码来源:https://blog.csdn.net/weialemon/article/details/78959125

9、strtotime()函数的使用

var_dump(date("Y-m-d", strtotime("last day of -1 month", strtotime("2017-03-31"))));
//输出2017-02-28

var_dump(date("Y-m-d", strtotime("first day of +1 month", strtotime("2017-08-31"))));
//输出2017-09-01

var_dump(date("Y-m-d", strtotime("first day of next month", strtotime("2017-01-31"))));
//输出2017-02-01

var_dump(date("Y-m-d", strtotime("last day of last month", strtotime("2017-03-31"))));
//输出2017-02-28
//代码来源:https://blog.csdn.net/qq_41620002/article/details/81450691

10、表示去年今日

$begin=date("Y-m-d", strtotime("-1 year", time()));

在这里插入图片描述
11、php获取指定日期的前一天、前几天、前一月、前一年的日期或时间戳
参考来源
(1)、前一天的日期为(如果要时间戳形式的,就不必转换成date):

date("Y-m-d",strtotime("-1 days",strtotime('2019-08-31')));

(2)、前一月的日期为:

date("Y-m-d",strtotime("-1 months",strtotime('2019-08-31')));

(3)、前一年的日期为:

date("Y-m-d",strtotime("-1 years",strtotime('2019-08-31')));

(4)、后一天的日期为:

date("Y-m-d",strtotime("+1 days",strtotime('2019-08-31')));

(5)、后一月的日期为:

date("Y-m-d",strtotime("+1 months",strtotime('2019-08-31')));

(6)、后一年的日期为:

date("Y-m-d",strtotime("+1 years",strtotime('2019-08-31')));

+1和-1是变量,根据需求改变

12、获取日期是星期几

$s_time = date('w',strtotime($post['s_time']));

13、获取两个日期之间的间隔天数

$between_days = floor((strtotime($post['e_time'])-strtotime($post['s_time']))/86400);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值