前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >强大的strtotime函数

强大的strtotime函数

作者头像
luxixing
发布2019-05-28 14:36:24
8920
发布2019-05-28 14:36:24
举报
文章被收录于专栏:luxixingluxixing

非常直观的一些用用法

  • 获取相比当前时间前N天的时间戳
代码语言:javascript
复制
$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天的时间戳
代码语言:javascript
复制
$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天的时间戳
代码语言:javascript
复制
$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点

其他一些用法

代码语言:javascript
复制
$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)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 非常直观的一些用用法
  • 其他一些用法
  • 一些strtotime中可以使用的关键词
    • 时间参数
      • 计算顺序
        • 时区
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档