V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sunus
V2EX  ›  PHP

php 的 strtotime 真神器

  •  
  •   sunus · 2015-03-25 14:17:52 +08:00 · 4756 次点击
    这是一个创建于 3312 天前的主题,其中的信息可能已经有所发展或是发生改变。

    每次用到其他语言解析日期时都不禁感慨一下

    20 条回复    2015-04-05 20:17:56 +08:00
    yaxin
        1
    yaxin  
       2015-03-25 14:21:10 +08:00
    哈哈,楼主,new DataTime()才是真正的神器呢
    kikyous
        2
    kikyous  
       2015-03-25 14:21:43 +08:00
    有ruby的
    3.days.from_now
    厉害吗?
    jarlyyn
        3
    jarlyyn  
       2015-03-25 14:34:57 +08:00
    javascript的moment.js不行么,浏览器里都能用。

    何况 php里还是datatime的object更好用点,和momenti.js差不多。
    cevincheung
        4
    cevincheung  
       2015-03-25 14:35:48 +08:00
    Floor 1 +10086
    DateTime才是大杀器
    msg7086
        5
    msg7086  
       2015-03-25 14:39:39 +08:00
    @cevincheung 你的第一行代码我看了很久……
    zhengkai
        6
    zhengkai  
       2015-03-25 14:45:39 +08:00
    @kikyous strtotime("+3 day")
    sunus
        7
    sunus  
    OP
       2015-03-25 15:15:16 +08:00
    @jarlyyn
    strtotime操作str,虽然没有操作date或者datetime规范,但觉得用起来方便直观,比如
    echo date('Y/m/d', strtotime('last day of next month'));
    echo date('Y/m/d', strtotime('fourth thursday of Nov'));
    jarlyyn
        8
    jarlyyn  
       2015-03-25 15:34:49 +08:00
    @sunus

    会来这个区的有谁不知道strtotime可以干啥么…………

    moment().add(1,'month').endOf('month').format('YYYY/MM/DD');

    moment().month('nov').add(4,'week').day('thursday').format('YYYY/MM/DD');

    datatime也有差不多的操作方式。
    cevincheung
        9
    cevincheung  
       2015-03-25 15:37:01 +08:00
    @msg7086 为啥
    GuangXiN
        10
    GuangXiN  
       2015-03-25 16:06:56 +08:00
    那天偶然看手册发现mktime可以支持“非法”的日期,我就惊了。比如传5月35号过去它会自动生成6月某一天的时间戳
    yueyoum
        11
    yueyoum  
       2015-03-25 16:09:17 +08:00
    python 的 arrow 才是真方便
    get
    to
    format
    replace
    oimotis
        12
    oimotis  
       2015-03-25 17:12:28 +08:00
    PHP是最好的语言,不服来战。。。
    joyeblue
        13
    joyeblue  
       2015-03-25 18:54:09 +08:00
    php strtotime 跟linux date命令是同源。
    zhujinliang
        14
    zhujinliang  
       2015-03-25 19:00:57 +08:00
    用一堆函数连起来搞的弱爆了,需要解析用户传参时就明白strtotime的好处了^_^
    lincanbin
        15
    lincanbin  
       2015-03-25 20:14:27 +08:00
    PHP的
    strtotime($str_time)
    真是丑爆了!

    还是我大Python优雅!
    int(time.mktime(time.strptime(str_time, "%Y-%m-%d %H:%M:%S")))
    jhdxr
        16
    jhdxr  
       2015-03-25 20:32:36 +08:00
    @lincanbin 你这例子还需要自己指定时间格式,呵呵。。。
    overflow
        17
    overflow  
       2015-03-25 23:23:16 +08:00
    不推荐使用 strtotime
    mingzepeng
        18
    mingzepeng  
       2015-03-26 13:16:35 +08:00
    @lincanbin 这种类型语法。。。php,java,js,ruby都是可以做到的。。。
    jarlyyn
        19
    jarlyyn  
       2015-03-26 13:17:36 +08:00
    @zhujinliang

    呵呵,不过滤用户数据的么?直接把用户输入丢进strtotime?

    用户数据必然是通过格式验证的可信数据才行。所以真正用起来才是datatime这种好用。
    mingyun
        20
    mingyun  
       2015-04-05 20:17:56 +08:00
    @overflow 为什么呢,比如?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5726 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 02:24 · PVG 10:24 · LAX 19:24 · JFK 22:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.