强大的strtotime函数

PHP处理时间的函数功能强大,相信大家都有目共睹,日常中如果涉及到很多时间处理方面的需求, strtotime函数的以下用法绝对让你面对时间处理不再发愁!

非常直观的一些用用法

  • 获取相比当前时间前N天的时间戳

$n = 2;//这个取值可以随意改变
$t = strtotime("-{$n} days");//相比于当前时间戳减去 $n*86400 s的时间
$t = strtotime("-{$n} days 00:00:00");//当前日期的前n天的0点
$t = strtotime("-{$n} days 23:00:00");//当前日期的前n天的23点
  • 获取相比当前时间后N天的时间戳

$n = 2;//这个取值可以随意改变
$t = strtotime("+{$n} days");//相比于当前时间戳加上 $n*86400 s的时间
$t = strtotime("+{$n} days 00:00:00");//当前日期的后n天的0点
$t = strtotime("+{$n} days 23:00:00");//当前日期的后n天的23点
  • 获取指定日期前/后N天的时间戳

$n = 2;//这个取值可以随意改变
$date = "2015-02-27";
$t = strtotime("+{$n} days {$date}");//相比于当前时间戳加上 $n*86400 s的时间
$t = strtotime("+{$n} days {$data} 00:00:00");//当前日期的后n天的0点
$t = strtotime("+{$n} days {$data} 23:00:00");//当前日期的后n天的23点

其他一些用法

$t = strtotime("yesterday");
$t = strtotime("yesterday midnight");
$t = strtotime("yesterday 00:00:00");
//以上三个结果一样,但是为了严谨期间,如果需要时分秒,给出精确时间
$t = strtotime("tomorrow");
$t = strtotime("tomorrow midnight");
$t = strtotime("tomorrow 00:00:00");

$year = 2015;
$month = 2;
$t = strtotime("first day of {$year}-{$month}");//指定月份的第一天
$t = strtotime("first day of {$year}-{$month} 11:00:00");//指定月份的第一天11点
$t = strtotime("last day of {$year}-{$month} 11:00:00");//指定月份的最后一天
$t = strtotime("last day of {$year}-{$month} 11:00:00");//指定月份的最后一天11点

以上仅仅是一小部分

ps

使用 +|- days[month|year] month和year可能出现的结果不是预期结果,慎用

一些strtotime中可以使用的关键词

时间参数
  • am(上午)

  • pm(下午)

  • year: "next year" 明年

  • month : "last month" 上个月

  • fortnight(两周) : "a fortnight ago" 两周前

  • week

  • day

  • hours

  • minute

  • second或者 sec

计算顺序

-ago 从当前时间往前计算

  • "24 hours ago"(hour也可以)

  • "3 years ago"(year也可以)

  • "12 month ago"

  • first-twelfth(连续的)

  • last

  • next

  • previos

时区
  • utc(标准0时区)

  • cct(中国东八区)

要想处理时间快,请认准 strtotime(str,timestamp)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值