By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,915 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,915 IT Pros & Developers. It's quick & easy.

initial value in a month array

P: 3
I have set up an array that includes the months of the year.

<?php
// define array
$month = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
// loop over it
// print array elements
foreach ($month as $a) {
echo '<br>'.$a;
}
?>

Is there a way to have this start with the current date?

I've seen date("m") used to pull the current date and have it set as the option value in a select box, but not in the array itself.

Any help would be greatly appreciated.
Sep 19 '06 #1
Share this Question
Share on Google+
3 Replies


P: 5
use the function now() and try
Sep 20 '06 #2

P: 3
This is the code I finally came up with.
Hope this helps someone else!

<?php
$month = array
('now -1 months',
'now -2 months',
'now -3 months',
'now -4 months',
'now -5 months',
'now -6 months',
'now -7 months',
'now -8 months',
'now -9 months',
'now -10 months',
'now -11 months',
'now -12 months');

foreach ($month as $a) {
$date=strtotime("$a");
$date=date("F",$date);
echo '<br>'.$date;
}
?>
Sep 20 '06 #3

P: 3
Below is the code that will pull current month first. The previous example was a month behind.

<?php
$month = array
('now',
'now -1 months',
'now -2 months',
'now -3 months',
'now -4 months',
'now -5 months',
'now -6 months',
'now -7 months',
'now -8 months',
'now -9 months',
'now -10 months',
'now -11 months');

foreach ($month as $a) {
$date=strtotime("$a");
$date=date("F",$date);
echo '<br>'.$date;
}
?>
Sep 20 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.