sunus
V2EX  ›  PHP

php 的 strtotime 真神器

  •  
  •   sunus · Mar 25, 2015 · 6304 views
    This topic created in 4247 days ago, the information mentioned may be changed or developed.

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

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

    何况 php里还是datatime的object更好用点,和momenti.js差不多。
    cevincheung
    cevincheung
    Mar 25, 2015
    4
    Floor 1 +10086
    DateTime才是大杀器
    msg7086
    msg7086
    Mar 25, 2015
    5
    @cevincheung 你的第一行代码我看了很久……
    zhengkai
    zhengkai
    Mar 25, 2015
    6
    @kikyous strtotime("+3 day")
    sunus
    sunus
    OP
    Mar 25, 2015
    7
    @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
    jarlyyn
    Mar 25, 2015
    8
    @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
    cevincheung
    Mar 25, 2015
    9
    @msg7086 为啥
    GuangXiN
    GuangXiN
    Mar 25, 2015
    10
    那天偶然看手册发现mktime可以支持“非法”的日期,我就惊了。比如传5月35号过去它会自动生成6月某一天的时间戳
    yueyoum
    yueyoum
    Mar 25, 2015
    11
    python 的 arrow 才是真方便
    get
    to
    format
    replace
    oimotis
    oimotis
    Mar 25, 2015
    12
    PHP是最好的语言,不服来战。。。
    joyeblue
    joyeblue
    Mar 25, 2015
    13
    php strtotime 跟linux date命令是同源。
    zhujinliang
    zhujinliang
    Mar 25, 2015
    14
    用一堆函数连起来搞的弱爆了,需要解析用户传参时就明白strtotime的好处了^_^
    lincanbin
    lincanbin
    Mar 25, 2015
    15
    PHP的
    strtotime($str_time)
    真是丑爆了!

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

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

    用户数据必然是通过格式验证的可信数据才行。所以真正用起来才是datatime这种好用。
    mingyun
    mingyun
    Apr 5, 2015
    20
    @overflow 为什么呢,比如?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2565 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 04:20 · PVG 12:20 · LAX 20:20 · JFK 23:20
    ♥ Do have faith in what you're doing.