Io ho un appuntamento in questo formato:Data meno 1 anno?
2009-01-01
Come faccio a restituire la stessa data, ma uno anno prima?
Io ho un appuntamento in questo formato:Data meno 1 anno?
2009-01-01
Come faccio a restituire la stessa data, ma uno anno prima?
Usa strtotime) Funzione (:
$time = strtotime("-1 year", time());
$date = date("Y-m-d", $time);
// set your date here
$mydate = "2009-01-01";
/* strtotime accepts two parameters.
The first parameter tells what it should compute.
The second parameter defines what source date it should use. */
$lastyear = strtotime("-1 year", strtotime($mydate));
// format and display the computed date
echo date("Y-m-d", $lastyear);
Utilizzando l'oggetto DateTime ...
$time = new DateTime('2099-01-01');
$newtime = $time->modify('-1 year')->format('Y-m-d');
oppure usando ora per oggi
$time = new DateTime('now');
$newtime = $time->modify('-1 year')->format('Y-m-d');
un modo più semplice che ho usato e lavorato bene
date('Y-m-d', strtotime('-1 year'));
questo ha funzionato perfetto .. spero che questo aiuti anche qualcun altro .. :)
È possibile utilizzare il followi Funzione ng per sottrarre 1 o qualsiasi anno da una data.
function yearstodate($years) {
$now = date("Y-m-d");
$now = explode('-', $now);
$year = $now[0];
$month = $now[1];
$day = $now[2];
$converted_year = $year - $years;
echo $now = $converted_year."-".$month."-".$day;
}
$number_to_subtract = "1";
echo yearstodate($number_to_subtract);
E guardando esempi di cui sopra è anche possibile utilizzare il seguente
$user_age_min = "-"."1";
echo date('Y-m-d', strtotime($user_age_min.'year'));
Non dimenticare di tendere alla questione semantica di ciò che si intende per "l'esercizio successivo" w.r.t. anni bisestili. Sottraendo 365 giorni dal 2008-02-28 ti fornirai il 2007-02-28, mentre sottraendo 365 giorni dal 2008-02-29 ti regalerai il 2007-03-31. – HostileFork
Immagino che dipenda molto da cosa significa "sottrarre un anno". Potresti dire lo stesso mese e giorno, ma un anno prima o il mese e il giorno dopo la sottrazione di 365 giorni, come indicato da Ostile. –