当前位置:首页 > 叨叨念念 > PHP时间日期以及时间戳事例

PHP时间日期以及时间戳事例

叨叨念念 / 星之宇 / 2021-8-30 9:43 / 浏览:2181 / 评论:0

date(format,timestamp) 函数可把时间戳格式化为可读性更好的日期和时间。。

format:必需。规定时间戳的格式。

timestamp:可选。规定时间戳。默认是当前的日期和时间。


strtotime(time,now) 函数将任何英文文本的日期时间描述解析为Unix时间戳。

time:必需。规定日期/时间字符串。

now:可选。规定用来计算返回值的时间戳。如果省略该参数,则使用当前时间。


mktime(hour,minute,second,month,day,year) 函数返回一个日期的UNIX时间戳。

hour:可选。规定小时。。

minute:可选。规定分。

second:可选。规定秒。

month:可选。规定月。

day:可选。规定天。

year:可选。规定年。


常用事例

当前时间戳:time() //1630280845

当前时间:date("Y-m-d H:i:s",time()) //2021-08-30 07:47:25


1.明天此时的时间戳:strtotime("+1 day")

date("Y-m-d H:i:s",strtotime("+1 day")) //2021-08-31 07:47:25


2.昨天此时的时间戳:strtotime("-1 day")

date("Y-m-d H:i:s",strtotime("-1 day")) //2021-08-29 07:47:25


3.下个星期此时的时间戳:strtotime("+1 week")

date("Y-m-d H:i:s",strtotime("+1 week")) //2021-09-06 07:47:25


4.上个星期此时的时间戳:strtotime("-1 week")

date("Y-m-d H:i:s",strtotime("-1 week")) //2021-08-24 07:47:25


5.下星期几的时间戳:strtotime("next Thursday")

date("Y-m-d H:i:s",strtotime("next Thursday")) //2021-09-02 07:47:25


6.上星期几的时间戳:strtotime("last Thursday")

date("Y-m-d H:i:s",strtotime("last Thursday")) //2021-08-26 07:47:25


7.当月开始时间戳:mktime(0,0,0,date('m'),1,date('Y'))

date("Y-m-d H:i:s",mktime(0,0,0,date('m'),1,date('Y')) //2021-08-01 00:00:00


8.当月结束时间戳:mktime(23,59,59,date('m'),date('t'),date('Y'))

date("Y-m-d H:i:s",mktime(23,59,59,date('m'),date('t'),date('Y')) //2021-08-31 23:59:59


8.上月开始时间戳:mktime(0,0,0,date('m',strtotime("-1 month")),1,date('Y',strtotime("-1 month")))

date("Y-m-d H:i:s",mktime(0,0,0,date('m',strtotime("-1 month")),1,date('Y',strtotime("-1 month"))) //2021-07-01 00:00:00


9.上个月结束时间戳:mktime(23,59,59,date('m',strtotime("-1 month")),date('t',strtotime("-1 month")),date('Y',strtotime("-1 month")))

date("Y-m-d H:i:s",mktime(23,59,59,date('m',strtotime("-1 month")),date('t',strtotime("-1 month")),date('Y',strtotime("-1 month")))) //2021-07-31 23:59:59


10.下月开始时间戳:mktime(0,0,0,date('m',strtotime("+1 month")),1,date('Y',strtotime("+1 month")))

date("Y-m-d H:i:s",mktime(0,0,0,date('m',strtotime("+1 month")),1,date('Y',strtotime("+1 month"))) //2021-09-01 00:00:00


11.下个月结束时间戳:mktime(23,59,59,date('m',strtotime("+1 month")),date('t',strtotime("+1 month")),date('Y',strtotime("+1 month")))

date("Y-m-d H:i:s",mktime(23,59,59,date('m',strtotime("+1 month")),date('t',strtotime("+1 month")),date('Y',strtotime("+1 month")))) //2021-09-30 23:59:59

目前有 0 条评论

    • 昵称
    • 邮箱
    • 网址