0

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
  • this works as expected. are you getting any errors? Commented Nov 25, 2009 at 8:45
  • This works now... Just placed the variable in a wrong place. Silly me! Commented Nov 25, 2009 at 8:48

3 Answers 3

2

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
)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

I try your code, no problem for me.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.