php - Strict standards error?

414

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;
}
657

Answer

Solution:

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

Answer

Solution:

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

People are also looking for solutions to the problem: php - Dynamic MySQL table name within a class

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.