PHP报告-严格标准:mktime():您应该使用time()函数

6

我有一个使用日期变量从MySQL数据库返回结果的PHP报告。

我没有编写该报告,它使用了mktime,最近我迁移到了新服务器,并安装了最新版本的php,现在出现以下错误:

这是创建日期变量的代码:

$start_date  = mktime(0,0,0,$StartMonth,$StartDay,$StartYear);
$end_date    = mktime(23,59,59,$EndMonth,$EndDay,$EndYear);

然后获取日期:

if ($HTTP_SERVER_VARS['REQUEST_METHOD'] == "POST") {
    if ($prefix == "Start") {
            $currYear = $StartYear;
            $currMonth = $StartMonth;
            $currDay = $StartDay;
    }
    elseif ($prefix == "End") {
            $currYear = $EndYear;
            $currMonth = $EndMonth;
            $currDay = $EndDay;
    }
}
else {
    $arr = getdate(mktime());
    $currYear = $arr["year"];
    $currMonth = $arr["mon"];

    // If the user hasn't chosen a date, 
    // set the beginning day at the first of the month
    if ($prefix == "Start")
            $currDay = 01;
    else
            $currDay = $arr["mday"];
}

当我现在运行报告时,我会得到“Strict Standards: mktime(): You should be using the time() function instead”错误。我已将其更改为“$arr = getdate(time());”,这样可以消除错误,但现在日期选择器无法使用。

http://www.php.net/manual/zh/function.mktime.php 说: 从 PHP 5.1 开始,当没有参数调用 mktime() 函数时,会抛出一个 E_STRICT 提示:应该使用 time() 函数代替。 - Tamil Selvan C
您可以尝试使用 print_r(getdate(mktime()))print_r(getdate(time())) 来检查是否存在可能会影响日期选择器的差异。 - Mariano D'Ascanio
我没有看到 $start_date$end_date 是如何使用的。你的问题只在于 $arr = getdate(mktime()); 这一行代码(和后续的日期选择器)吗?你能将你的代码简化成只有这部分吗? - Teepeemm
问题出在 $arr = getdate(mktime()); 这一行。如果我把它改成 time(),它就不起作用了。我会放弃使用那个数组来获取日期,并尝试只使用日期选择器来完成它。 - tagteam
你能否发布一下声明 $arr 的代码? - Adam Copley
1个回答

5

不带参数调用 mktime() 等同于调用 time()。函数声明如下:

int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )

换句话说,不使用参数与使用当前日期相同,这就是 time() 所做的事情。
$ php -a
Interactive shell

php > echo mktime();

Strict Standards: mktime(): You should be using the time() function instead in php shell code on line 1
1450208188
php > echo time();
1450208189
php > 

如果你把mktime()改为time(),并不会导致任何错误。问题可能出在其他地方。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接