2011-05-05 12 views
32

¿Cómo soy capaz de obtener el valor del año anterior usando PHP. ¿Hay alguna función predefinida para ello?Como ir año anterior usando PHP

+3

¿Cuál es el problema? '$ = $ PreviousYear año - 1; 'o algo así como' $ prevYear = date ('Y', $ timestamp) - 1; ' – KingCrunch

+0

algo así como $ prevYear = date ('Y', $ timestamp) - 1; – Fero

Respuesta

81

tratar

echo date("Y",strtotime("-1 year")); 
+0

acabas de hacer que php sea un poco menos terrible para mí –

2
function adddate($vardate,$added) 
{ 
$data = explode("-", $vardate); 
$date = new DateTime();    
$date->setDate($data[0], $data[1], $data[2]); 
$date->modify("".$added.""); 
$day= $date->format("Y-m-d"); 
return $day;  
} 

echo "Example : " . adddate("2010-08-01","-1 year"); 
5

Hay muchas maneras, puede quitar la cantidad de segundos en un año a partir de time() así:

$prevYear = date('Y', time() - 60*60*24*365);

O si lo' d prefiero, utilice la función PHPs strtotime() inteligente:

$prevYear = date('Y', strtotime('-1 year'));

o incluso como otros han dicho, si es de hoy año que acaba de hacer date('Y') -1

+0

Esta fecha ('Y', time() - 60 * 60 * 24 * 365) debe usarse con cuidado, porque los años bisiestos crearán problemas. – Muk

1

Si desea mostrar toda la fecha exactamente hace 1 año, el mes, inclusive y el año:

<?php echo date("M d Y", strtotime("-1 year")); ?> 
0

enfoque más corta:

$lastYear = date("Y") - 1; 
-1

Trate Esta

date('Y', strtotime('last year')); 
Cuestiones relacionadas