Connecting Tech Pros Worldwide Forums | Help | Site Map

initial value in a month array

Newbie
 
Join Date: Sep 2006
Posts: 3
#1: Sep 19 '06
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.

Newbie
 
Join Date: Aug 2006
Location: India - chennai
Posts: 5
#2: Sep 20 '06

re: initial value in a month array


use the function now() and try
Newbie
 
Join Date: Sep 2006
Posts: 3
#3: Sep 20 '06

re: initial value in a month array


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;
}
?>
Newbie
 
Join Date: Sep 2006
Posts: 3
#4: Sep 20 '06

re: initial value in a month array


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;
}
?>
Reply