strtotime()函数:把日期转换成时间戳;
1,strtotime()时间转化
$datetime=strtotime(date());
date("Y-m-d H:i",$datetime)
2,获得今天零点的时间戳
第一步,先获取unix时间戳
$todaytime=strtotime("today")
第二步,再转化成可读日期
使用的时候一定要注意标点符号,英文输入下的双引号
date("Y-m-d H:i:s",$todaytime)
日期格式:"Y-m-d H:i:s" H小时(大写为24小时),i分,s秒
"Y-m-d"只显示年月日;
如:
$todaytime=strtotime("today");
echo '今天开始时间:'.date("Y-m-d H:i:s",$todaytime);
输出结果:
今天开始时间:2021-11-05 00:00:00
获取今天23:59:59
① $nextdaytime=strtotime(date('Y-m-d',time()))+24*60*60-1;
② $nextdaytime=strtotime(date('Y-m-d 23:59:59',time()));
3,常用格式:strtotime()时间转化
获取今天格式
echo strtotime('Today');
echo strtotime(date('Y-m-d'));
获取明天凌晨的时间戳
echo strtotime(date('Y-m-d',strtotime('+1 day')));
其它格式:一看就懂的不再加详细解释:
echo "一周后:".date("Y-m-d",strtotime("+1 week"));
echo "一周零一天一小时一秒后:".date("Y-m-d G:H:s",strtotime("+1 week 1 days 1 hours 1 seconds"));
echo "下个星期一:".date("Y-m-d",strtotime("next Monday"));
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("+1 year"));
在实践中不断的成长,把每一个经历都记录下来,给需要的人一点微薄的帮助,在代码历程上少走弯路。