【问题标题】:Converting VB DATEADD() function into a PHP equivalent将 VB DATEADD() 函数转换为 PHP 等效函数
【发布时间】:2011-10-12 08:47:32
【问题描述】:

我需要将以下行转换为 PHP,但不确定 DATEADD() 等效项以及需要如何显示参数。

这是为 PHP 稍微修改了 IF ELSE 语句的脚本:

if ($period == '1 month') 
        $finish = DATEADD(m, 1, $start);

    else if ($period == '1 year') 
        $finish = DATEADD(yy, 1, $start);

    else
        $finish = DATEADD(d, 1, $start);

谢谢

【问题讨论】:

    标签: php datetime vbscript


    【解决方案1】:

    看看strtotime()

    <?php
    
    $start = time(); // your time origin
    $result = strtotime("+1 month", $start); // respects 28/30/31 days
    $result = strtotime("next monday", $start); // getting silly…
    

    -- 编辑--

    如果你喜欢对象,你也可以给DateTime::add()一个机会。不过,不知道为什么这会比 strtotime 方法更好。

    <?php
    
    $start = new DateTime();
    $start->add(new DateInterval("P1M")); // add 1 month
    

    【讨论】:

    • DateTime::add() 方法比strtotime() 更好,因为strtotime() 是一个尝试做所有事情的旧函数。它非常慢并且有一些令人讨厌的怪癖,而DateTime::add() 更新更快且不那么古怪。您甚至不必将其用作对象;您可以简单地调用 date_add() 以获取调用相同函数的非 OO 方式。
    • 你在说 strtotime() 有什么怪癖,@Spudley?
    • strtotime() 有很多不好的地方。首先,它处理dd/mm/yyyymm/dd/yyyy 格式之间的歧义的方式令人恐惧。然后是这样的彻底错误:stackoverflow.com/questions/7724954/…
    猜你喜欢
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    相关资源
    最近更新 更多