zoukankan      html  css  js  c++  java
  • PHP strtotime() 函数

    ------------恢复内容开始------------

    实例

    将任何字符串的日期时间描述解析为 Unix 时间戳:

    <?php // 设置时区 date_default_timezone_set("PRC"); $time = strtotime("2018-01-18 08:08:08"); // 将指定日期转成时间戳 // 打印当前时间 PHP_EOL 换行符,兼容不同系统 echo $time, PHP_EOL; // 更多实例 echo strtotime("now"), PHP_EOL; echo strtotime("now"), PHP_EOL; echo strtotime("10 September 2000"), PHP_EOL; echo strtotime("+1 day"), PHP_EOL; echo strtotime("+1 week"), PHP_EOL; echo strtotime("+1 week 2 days 4 hours 2 seconds"), PHP_EOL; echo strtotime("next Thursday"), PHP_EOL; echo strtotime("last Monday"), PHP_EOL; ?>

    输出结果:

    1516234088
    1517408272
    1517408272
    968515200
    1517494672
    1518013072
    1518200274
    1517414400
    1517155200

    定义和用法

    strtotime() 函数将任何字符串的日期时间描述解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数)。

    注意:如果年份表示使用两位数格式,则值 0-69 会映射为 2000-2069,值 70-100 会映射为 1970-2000。

    注意:请注意 m/d/y 或 d-m-y 格式的日期,如果分隔符是斜线(/),则使用美洲的 m/d/y 格式。如果分隔符是横杠(-)或者点(.),则使用欧洲的 d-m-y 格式。为了避免潜在的错误,您应该尽可能使用 YYYY-MM-DD 格式或者使用 date_create_from_format() 函数。

    语法

    int strtotime ( string $time [, int $now = time() ] )
    参数描述
    time 必需。规定日期/时间字符串。
    now 可选。规定用来计算返回值的时间戳。如果省略该参数,则使用当前时间。

    技术细节

    返回值: 成功则返回时间戳,失败则返回 FALSE。
    PHP 版本: 4+高佣联盟 www.cgewang.com
    更新日志: PHP 5.3.0:现在相对时间格式,比如这一周、前一周、上一周、下一周,规定一周从星期一到星期日,而不是使用相对于当前日期/时间的前后 7 天。
    PHP 5.3.0: 现在 24:00 是有效的格式。
    PHP 5.2.7:在这之前的版本中,如果请求一个月中某个给定日期且该日期刚好是该月的第一天,则会错误地把增加一个星期到返回的时间戳中,这点现在已经得到更正。
    PHP 5.1.0:如果失败则返回 FALSE(在这之前的版本则返回 -1),并增加了 E_STRICT 和 E_NOTICE 时区错误。
    PHP 5.0.2:现在正确计算 "now" 和其他相对时间是以当前时间为准,而不是以今天午夜的时间为准。
    PHP 5.0.0:允许微秒(但微秒数通常被忽略)。

    ------------恢复内容结束------------

  • 相关阅读:
    Linux tcpdump 命令详解与示例
    Linux 查看磁盘IO并找出占用IO读写很高的进程
    Rsync 服务部署与参数详解
    Linux curl 表单登录或提交与cookie使用
    Linux curl 常用示例
    Linux curl 命令详解
    Linux下使用 github+hexo 搭建个人博客07-next主题接入搜索和站点管理
    Linux下使用 github+hexo 搭建个人博客06-next主题接入数据统计
    Linux下使用 github+hexo 搭建个人博客05-next主题接入评论系统
    Linux下使用 github+hexo 搭建个人博客04-next主题优化
  • 原文地址:https://www.cnblogs.com/yc10086/p/12991129.html
Copyright © 2011-2022 走看看