tags:

views:

41

answers:

3

Hi there, I have a date format like this :

  $date1 = "Sun May 09 20:07:50 +0000 2010";

and I have to convert it to: 09-05-2010

I am echoing it with echo date("d-m-Y", strtotime($date1));

When I print this individually it gives proper result but I am using it in loop it gives me results like: 31-12-1969

The loop i am using is :

foreach($userinfo as $k=>$v)
{
if($k == 'test')
 {
    foreach($v as $k1=>$v1)
    {
      echo $v1."<br>";
      //echo strtotime($v1)."<br>";
      //echo $date = date("d-m-Y", strtotime($v1));
    }
 }
}

Guys when I echo $v1 it gives me:Sun May 09 20:07:50 +0000 2010 Also when I echo strtotime($v1); it gives blank. However funny thing is I am getting this conversion perfectly in 1D loop.

Can you help, please?

+1  A: 
echo date("d-m-Y", strtotime($date1)) ;
mononym
question has changed, can you show us the loop?
mononym
you have to use strtotime() as the [date()](http://php.net/manual/en/function.date.php) function expects a timestamp as its second parameterI just ran your loop and it works fine, its obviously your input, instead of echoing the date, echo $v1 and give us you're values
mononym
thank you guys for your support, i could solve the problem with your valuable quick help. Thank you again!!!!
Rishi2686
A: 

Is $date1 = "Sun May 09 20:07:50 +0000 2010"; outside the loop? If it is, make sure you only change it with strtotime and date once.

Emil Vikström
It is inside the loop, each user's date should convert into that format.
Rishi2686
thank you guys for your support, i could solve the problem with your valuable quick help. Thank you again!!!!
Rishi2686
A: 

First, you could simplify by writing :

foreach ($userinfo['test'] as $k => $v) {
    foreach ($v as $k1 => $v1) {
        echo date("d-m-Y", strtotime($v1));
    }   
}

The second strtotime() argument is not useful for you, it is in case you use the function to make operations on timestamps, like :

echo strtotime("-1 day", mktime(...));

Just do print_r($userinfo['test']) to see what's really inside.

mexique1
thank you guys for your support, i could solve the problem with your valuable quick help. Thank you again!!!!
Rishi2686