arrays and date in php

Go To StackoverFlow.com

0

My var_dump looks like this, I know that I'm getting the error because I'm passing an array instead of a string. But how can I solve it?

array
  0 => 
    array
      'date_submitted' => string '2012-03-22 19:28:22' (length=19)
  1 => 
    array
      'date_submitted' => string '2012-03-28 21:31:28' (length=19)

My function

function getArticle() {

    $article = new Article('x');
    $arr = $article->getArticles();

    $date= null;
    foreach($arr as $record){
        $currentDate = date('l, F j', strtotime($record['date_submitted']));

        if ($currentDate != $date) {
            echo set_format_date($record['date_submitted'],'l, F j');
        }
        $date = $currentDate;
    }
}

Tried using $x=0; then $x++; but that returns Undefined offset: 0 like this $record[$x]['date_submitted']; , it didnt work.

2012-04-03 20:39
by SupaOden
"the error" - what is this "the error"? You're not passing an array to strtotime, you're passing a string member of an array, so it can't be that - Marc B 2012-04-03 20:41
What variable are you var_dumping? - PenguinCoder 2012-04-03 20:42
@PenguinCoder I created an array called $arr, and using while looping thouch each database record and using $result->fetch_assoc(), I pushed an array to another array, $ar - SupaOden 2012-04-03 20:44
Poster is probably var_dump $arr which contains all the Article - Khôi 2012-04-03 20:47
You should var_dump($record); within the loop to see why it's telling you that - Crashspeeder 2012-04-03 20:49
I don't think the error lies at the strtotime call. Please show us the code for set_format_date as well - since this function isn't bundled with vanilla php - Khôi 2012-04-03 20:51
I dont know what I did but it fixed the error. Strange. Code is still the same - SupaOden 2012-04-03 20:54


0

if you wish to manipulate the array itself inside a foreach loop, you have to reference the value.

foreach($arr as &$record){
  // ...
}

if you manipulate $record now inside the loop, $arr will actually contain the values you manipulated. otherwise $record is just in scope of the loop.

2012-04-03 21:44
by thrau