2013-08-16 28 views
40

ho questo codice PHP:data PHP aggiungere 1 anno data corrente

$end=date('Y-m-d'); 

Io lo uso per ottenere la data corrente, e ho bisogno la data di 5 anni nel futuro, qualcosa come:

$end=date('(Y + 5)-m-d'); 

Come posso fare questo?

+0

possibile duplicato di [aggiungi un anno per datetime con php] (http://stackoverflow.com/questions/5212240/add-one-year-to -datetime-with-php) –

+0

Diventa leggermente eccentrico il 29 febbraio in un anno bisestile quando improvvisamente arriva il 1 ° marzo - essere consapevoli di tali stranezze quando si cerca di manipolare le date –

risposta

99

Prova con:

$end = date('Y-m-d', strtotime('+5 years')); 
+0

Ciao, ho bisogno di usare questo esempio per aggiungere 1 anno ad una data, ma ho già variabili: '$ fatturate = $ _POST ['invdate']; $ invdate = date ("Ymd", strtotime ($ invoicedate)); 'quindi dovrei aggiungere una terza riga' $ due = date ('Ym-d', strtotime ($ invdate, '+ 1 anno')); '? – Adrian

1

Per aggiungere un anno per la data di oggi utilizzare il seguente:

$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " + 365 day")); 
+1

e per gli anni bisestili? – suspectus

+0

Questo è stato bello grazie mille :) sto accettando qualche momento dopo – Letter

+0

@Letter Guarda la mia risposta - è molto più semplice. – hsz

0

Utilizzando Carbon:

$dt = Carbon::now(); 
echo $dt->addYears(5); 
0

provare questo,

$presentyear = '2013-08-16 12:00:00'; 

$nextyear = date("M d,Y",mktime(0, 0, 0, date("m",strtotime($presentyear)), date("d",strtotime($presentyear)), date("Y",strtotime($presentyear))+5)); 

echo $nextyear; 
12

Modifica datebased on this post
strtotime() è davvero potente e consente di modificare/trasformare date facilmente con le sue espressioni relative troppo:

procedurale

$dateString = '2011-05-01 09:22:34'; 
    $t = strtotime($dateString); 
    $t2 = strtotime('-3 days', $t); 
    echo date('r', $t2) . PHP_EOL; // returns: Thu, 28 Apr 2011 09:22:34 +0100 

DateTime

$dateString = '2011-05-01 09:22:34'; 
    $dt = new DateTime($dateString); 
    $dt->modify('-3 days'); 
    echo $dt->format('r') . PHP_EOL; // returns: Thu, 28 Apr 2011 09:22:34 +0100 

Le cose che puoi lanciare a strtotime() sono abbastanza sorprendenti e leggibili. Dai un'occhiata a questo esempio cercando Martedì la prossima settimana.

procedurale

$t = strtotime("Tuesday next week"); 
    echo date('r', $t) . PHP_EOL; // returns: Tue, 10 May 2011 00:00:00 +0100 

DateTime

$dt = new DateTime("Tuesday next week"); 
    echo $dt->format('r') . PHP_EOL; // returns: Tue, 10 May 2011 00:00:00 +0100 

noti che questi esempi sopra vengono restituiti relativamente al tempo. L'elenco completo dei formati di orario utilizzati da strtotime() e dal costruttore DateTime sono elencati su PHP Supported Date and Time Formats page.

Un altro esempio, adatto per il vostro caso potrebbe essere:based on this post

<?php 
    //How to get the day 3 days from now: 
    $today = date("j"); 
    $thisMonth = date("n"); 
    $thisYear = date("Y"); 
    echo date("F j Y", mktime(0,0,0, $thisMonth, $today+3, $thisYear)); 

    //1 week from now: 
    list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y")); 
    echo date("F j Y", mktime(0,0,0, $thisMonth, $today+7, $thisYear)); 

    //4 months from now: 
    list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y")); 
    echo date("F j Y", mktime(0,0,0, $thisMonth+4, $today, $thisYear)); 

    //3 years, 2 months and 35 days from now: 
    list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y")); 
    echo date("F j Y", mktime(0,0,0, $thisMonth+2, $today+35, $thisYear+3)); 
    ?> 
6

Usa questo codice per aggiungere anni o mesi o giorni o ore o minuti o secondi di una data determinata

echo date("Y-m-d H:i:s", strtotime("+1 years", strtotime('2014-05-22 10:35:10'))); //2015-05-22 10:35:10 
echo date("Y-m-d H:i:s", strtotime("+1 months", strtotime('2014-05-22 10:35:10')));//2014-06-22 10:35:10 
echo date("Y-m-d H:i:s", strtotime("+1 days", strtotime('2014-05-22 10:35:10')));//2014-05-23 10:35:10 
echo date("Y-m-d H:i:s", strtotime("+1 hours", strtotime('2014-05-22 10:35:10')));//2014-05-22 11:35:10 
echo date("Y-m-d H:i:s", strtotime("+1 minutes", strtotime('2014-05-22 10:35:10')));//2014-05-22 10:36:10 
echo date("Y-m-d H:i:s", strtotime("+1 seconds", strtotime('2014-05-22 10:35:10')));//2014-05-22 10:35:11 

Puoi anche sottrarre sostituendo + a -

0

È molto molto facile con Car bon. $date = "2016-02-16"; // Or Your date $newDate = Carbon::createFromFormat('Y-m-d', $date)->addYear(1);

+0

La classe di carbonio e i suoi metodi appartengono a un particolare lavoro di frame (Laravel) e nella domanda posta non troviamo alcun nome di lavoro di frame piuttosto che viene chiesto utilizzando le funzioni di data di PHP. Penso che sia per questo che non hai avuto i pollici :) –

-1

provare questo:

$yearnow= date("Y"); 
$yearnext=$yearnow+1; 
echo date("Y")."-".$yearnext; 
+0

Non proprio quello che è stato chiesto. – wonce

0
 $date = strtotime($row['timestamp']); 
     $newdate = date('d-m-Y',strtotime("+1 year",$date)); 
Problemi correlati