Press "Enter" to skip to content

PHP时间戳转换的方法

日期转时间戳

  • mktime()
mktime($hour,$minute,$second,$month,$day,$year);

# 例子:
echo mktime(0,0,0,6,7,2017); //2017年6月7日00:00:00
>> 1496764800
  • strtotime()
strtotime($date);

# 例子:
echo strtotime('2017-6-7 00:00:00'); //2017年6月7日00:00:00
>> 1496764800

对日期进行加减

  • 对当前时间加减
// 当前时间加一个月
strtotime('+1 month');

// 当前时间减一个月
strtotime('-1 month');

// 当前时间加一年
strtotime('+1 year');
>> 以上均返回时间戳

# 例子:返回最近三个月的月份
echo date("Ym",strtotime(date('Ym').' -1 month')); //上一月
echo date("Ym",strtotime(date('Ym').' -2 month')); //上两月
echo date("Ym",strtotime(date('Ym').' -3 month')); //上三月
发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注