简体   繁体   English

严格的标准错误?

[英]Strict standards error?

The code below was working correctly a time ago, however, when I tried to use it now, it says that there is an error in this line : 下面的代码在一段时间前可以正常工作,但是,当我现在尝试使用它时,它表示此行有错误:

 $date1 = new DateTime(array_shift(array_values($array_of_dates)));

The error says : 错误说:

Strict Standards: Only variables should be passed by reference in /home/...

Below is my code : 下面是我的代码:

public function get_user_weeks($user_id,$getdays = NULL,$before_num_days = NULL) {

$weeks_between =0;
$pgql = mysql_query("SELECT at_time FROM users WHERE track_id='$user_id' ORDER BY at_time ASC");
$array_of_dates = array();
while ($row = mysql_fetch_array($pgql, MYSQL_ASSOC)) {
    $array_of_dates[] = $row['at_time'];
}

$date1 = new DateTime(array_shift(array_values($array_of_dates)));
 if ($before_num_days==NULL) {
$date2 = new DateTime(date("Y-m-d h:m:s")); }
else {



    $date2 = date("Y-m-d h:m:s");
    $date2 = strtotime('+'.$before_num_days.' day', strtotime($date2));
    $date2 = date('Y-m-d h:m:s',$date2);
    $date2 = new DateTime($date2);

    }
$interval = $date1->diff($date2);
if(NULL == $getdays) {
$weeks_between = (($interval->d) + (30.5 * $interval->m) + (365 * $interval->y))/7; }
else {
$weeks_between = (($interval->d) + (30.5 * $interval->m) + (365 * $interval->y));
    }

return $weeks_between;
}

It is triggered by array_shift() , because it needs a variable, not a value, as an argument. 它由array_shift()触发,因为它需要一个变量而不是一个值作为参数。

$values = array_values($array_of_dates);
$value  = array_shift($values);
$date1  = new DateTime($value);

Also, please note that MySQL extension is officially deprecated one. 另外,请注意,MySQL扩展已被正式弃用。 Use MySQLi or PDO instead. 改用MySQLiPDO

array_shift takes a reference, so you need to change your code like this: array_shift需要一个参考,因此您需要像这样更改代码:

$values = array_values($array_of_dates);
$values = array_shift($values);
$date1 = new DateTime($values);

That is because a reference is returned... In such case, assign that to a variable and overcome this issue. 这是因为返回了引用...在这种情况下,请将其分配给变量并解决此问题。

You can do like this... 你可以这样...

$date1 = new DateTime($dt=array_shift(array_values($array_of_dates))); //Check the variable $dt ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM