php中对一个时间戳加上一个日期,只需要在指定的时间戳上加上一个指定的秒数即可。但在某些情况下是不能这样操作的。比如给一个用户加上一个VIP会员的时间,由于每个月或每一年的天数是不同的,就需要直接加一个月或一年,不然修改后的时间会错乱的。
php strtotime() 函数
使用php预设的 strtotime() 函数,可以对指定的时间进行加减,比如加一周或一月并解析为 Unix 时间戳:
语法:strtotime(time,now);
参数:
time:规定日期/时间字符串
now:规定用来计算返回值的时间戳,如果填写,则默认为当前时间
php 在当前时间上加一天,一周,一月,一年的方法
php代码:<?php
$d1 = strtotime("+1 hours"); //加5小时
$d2 = strtotime("+1 day"); //加5小时
$d3 = strtotime("+1 week"); //加一周
$d4 = strtotime("+1 Monday"); //加一个月
$d5 = strtotime("+1 Year"); // 加一年
//格式化时间
echo date('Y-m-d H:i:s',$d1);
echo date('Y-m-d H:i:s',$d2);
echo date('Y-m-d H:i:s',$d3);
echo date('Y-m-d H:i:s',$d4);
echo date('Y-m-d H:i:s',$d5);
?>
php 在指定的时间上加一天,一周,一月,一年的方法
在指定的时间上,加个一天,一周或一月一年和上面的代码是一样的,只需要设置 strtotime 函数的第二个参数即可!
php代码:<?php
$t = '1611640456'; //这是一个指定时间的时间戳
$d1 = strtotime("+1 hours" , $t); //加5小时
$d2 = strtotime("+1 day" , $t); //加5小时
$d3 = strtotime("+1 week" , $t); //加一周
$d4 = strtotime("+1 Monday" , $t); //加一个月
$d5 = strtotime("+1 Year" , $t); // 加一年
echo date('Y-m-d H:i:s',$d1);
echo date('Y-m-d H:i:s',$d2);
echo date('Y-m-d H:i:s',$d3);
echo date('Y-m-d H:i:s',$d4);
echo date('Y-m-d H:i:s',$d5);
?>