my code

Calculates the month and year from a starting month and year minus a number of months.

Snippet options

Download: Download snippet as month-calculation.php.
Copy snippet: For this you need a free my code account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

/* Funcion para obtener un periodo de meses en formato MMMAA/MMMAA*/
function obtenerMeses($numeromeses, $conexion){
	$AAActual = date(y);
	$MMActual = $_SESSION['mes_activo'];

	$sqlMes = mysql_query("SELECT id, ncorto FROM meses WHERE nlargo = '$MMActual'", $conexion);
	$arrMes = mysql_fetch_array($sqlMes);

	$AA = $AAActual;
	$MM = $arrMes['id'];
	$MM = $MM + 1;
	$MMActual = $arrMes['ncorto'];

	while($i <= $numeromeses){
		$MM -= 1;
			$MM = 12;
			$AA -= 1;

		$i += 1;

	$sqlMes = mysql_query("SELECT ncorto FROM meses WHERE id = '$MM'", $conexion);
	$arrMes = mysql_fetch_array($sqlMes);
	$MM = $arrMes['ncorto'];

	if($AA < 10){
		$AA = "0$AA";

} // fin función obtenerMesesFact

Create a free my code account now.

my code is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.