Format php date with a variable
I got a code like this that works just fine.
$dates[] = date('F, Y', $date);
I wond开发者_运维技巧er 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.
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
)
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.
I try your code, no problem for me.
Are you sure that your date is a time ? with the function time for example ?
精彩评论