0

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;
}
shikata
  • 95
  • 1
  • 11
  • possible duplicate of [Strict Standards: Only variables should be passed by reference](http://stackoverflow.com/questions/2354609/strict-standards-only-variables-should-be-passed-by-reference) – Lorenz Meyer Jun 21 '14 at 08:33

3 Answers3

3

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.

BlitZ
  • 12,038
  • 3
  • 49
  • 68
1

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);
Panda
  • 6,955
  • 6
  • 40
  • 55
laurent
  • 88,262
  • 77
  • 290
  • 428
0

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 ;)
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126