简体   繁体   中英

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.

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

Also, please note that MySQL extension is officially deprecated one. Use MySQLi or PDO instead.

array_shift takes a reference, so you need to change your code like this:

$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 ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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