tags:

views:

54

answers:

3

Hi!

I got a code like this that works just fine.

$dates[] = date('F, Y', $date);

I wonder if it's possible to pass a variable to the first argument. Something like this (but this doesn't work):

$date_format = 'F, Y';
$dates[] = date($date_format, $date);

EDIT: This actually works just fine. Just placed the variable in the wrong place.

+2  A: 

That's perfectly legal. As to why it doesn't work, can you provide a code snippet that doesn't work? There will be some other reason why. I run this:

$date_format = 'F, Y';
$inputs = array(time(), time() + 5000000, time() + 10000000);
$dates = array();
foreach ($inputs as $input) {
  $dates[] = date($date_format, $input);
}
print_r($dates);

and get:

Array
(
  [0] => November, 2009
  [1] => January, 2010
  [2] => March, 2010
)
cletus
+1  A: 

date() just takes a string as it's first argument. Whether it is a literal string like your first example or a variable containing a string like the second example doesn't matter - they are equivilent.

Splash
A: 

I try your code, no problem for me.

Are you sure that your date is a time ? with the function time for example ?

Kiva