Forum Moderators: coopster

Message Too Old, No Replies

Adding Months in PHP

         

dainstructor

9:32 pm on Jan 3, 2007 (gmt 0)

10+ Year Member



I am looking for a way to add and subtract months in php. I would like to dynamically adjust my table based on what month it is. I currently have:

<?php echo(date("M")?>

and I would like to list the rest of the months out in the remainder of the table.

I've unsuccessfully tried:

<?php echo(date("M")+1)?>
<?php echo(date("M")+2)?>
etc...

Does any one know of a function that will handle this?

Thanks

whoisgregg

9:56 pm on Jan 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Personally I prefer strtotime [php.net]:

echo date("M",strtotime("+1 month"));
echo date("M",strtotime("+2 months"));

The manual page for date [php.net] suggests you can also use mktime [php.net], but it seems like it'd be a hassle to jump across years that way so I always use strtotime.

jatar_k

10:05 pm on Jan 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could use a prepopulated array of month names

once you figure out which month it is then you could walk the array from the point where the present month occurs

would be less function calls, though it may only be a small performance difference

dainstructor

2:10 pm on Jan 4, 2007 (gmt 0)

10+ Year Member



Thanks guys, I used whoisgregg's approach and it seems to work perfectly.

The idea of using an array seems pretty useful as well, i'll certainly keep that in mind.

Cheers!