不言不语

您现在的位置是: 首页 >  PHP

PHP

PHP获取零点的时间戳

2022-05-31PHP
今天项目中,需要知道当天零点的时间戳的信息,所以想获得当天零点的时间戳,随便复习下PHP中时间戳的相关知识点,把相关知识点作了下总结,总结如下:

今天项目中,需要知道当天零点的时间戳的信息,所以想获得当天零点的时间戳,随便复习下时间戳的相关知识点,把相关知识点作了下总结,总结如下:

<?php

header("Content-type:text/html;charset=utf-8");


//设置中国时间为默认时区
date_default_timezone_set('PRC');


//获得当天凌晨的时间戳
$today = strtotime(date("Y-m-d"),time());
echo '获得当天凌晨的时间戳: '.$today;
echo '<br>';


// 同理,我们就可以获取到了昨天凌晨零点的时间戳
// 今天凌晨零点的时间戳 减去 一天的秒数
$yesterday = strtotime(date("Y-m-d"),time())-3600*24;
echo '获得昨天凌晨的时间戳: '.$yesterday;
echo '<br>';


// 相对的,我们也可以获取到了明天凌晨零点的时间戳
// 今天凌晨零点的时间戳 加上 一天的秒数
$tomorrow = strtotime(date("Y-m-d"),time())+3600*24;
echo '获得明天凌晨的时间戳: '.$tomorrow;
echo '<br>';


// 同理,这个月第一天凌晨零点的时间戳
$month = strtotime(date("Y-m"),time());
echo '获得这个月第一天凌晨的时间戳: '.$month;
echo '<br>';


// 这一年第一天凌晨零点的时间戳
// 注意: 这里只用 strtotime(date("Y"),time());行不通,最好加上月份
$year = strtotime(date("Y-01"),time());
echo '获得这一年第一天凌晨的时间戳: '.$year;
echo '<br>';

效果图:

1231.jpg

其它参考代码:

echo "一周后:".date("Y-m-d",strtotime("+1 week"));     
echo "一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds"));     
echo "下个星期四:".date("Y-m-d",strtotime("next Thursday"));     
echo "上个周一:".date("Y-m-d",strtotime("last Monday"));     
echo "一个月前:".date("Y-m-d",strtotime("last month"));     
echo "一个月后:".date("Y-m-d",strtotime("+1 month"));     
echo "十年后:".date("Y-m-d",strtotime("+10 year"));

12342.jpg


文章评论