2010-03-24 18 views

risposta

20

strtotime è tuo amico

echo strtotime("-1 week"); 
+0

Hai strotime errore di battitura per strtotime .. :) troppo presto solo 5 anni fa ...: D –

+0

@Yegya Ha! Risolto: P –

6

C'è il seguente esempio a PHP.net

<?php 
    $nextWeek = time() + (7 * 24 * 60 * 60); 
       // 7 days; 24 hours; 60 mins; 60secs 
    echo 'Now:  '. date('Y-m-d') ."\n"; 
    echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n"; 
    // or using strtotime(): 
    echo 'Next Week: '. date('Y-m-d', strtotime('+1 week')) ."\n"; 
?> 

Cambiare + a - sulla prima (o ultima) linea otterrà ciò che desideri.

-1
<?php 
    $before_seven_day = $date_timestamp - (7 * 24 * 60 * 60) 
    // $date_timestamp is the date from where you found to find out the timestamp. 
?> 

è anche possibile utilizzare la funzione stringa su ora per convertire la data in data e ora. come

strtotime(23-09-2013); 
+1

@AndrewBarber La risposta potrebbe essere stata modificata, ma questa risposta non sembra includere un collegamento a qualcosa. – starbeamrainbowlabs

3

Da PHP 5.2 è possibile utilizzare DateTime:

$timestring="2015-03-25"; 
$datetime=new DateTime($timestring); 
$datetime->modify('-7 day'); 
echo $datetime->format("Y-m-d"); //2015-03-18 

Invece di creare DateTime con stringa, è possibile setTimestamp direttamente sul oggetto:

$timestamp=1427241600;//2015-03-25 
$datetime=new DateTime(); 
$datetime->setTimestamp($timestamp); 
$datetime->modify('-7 day'); 
echo $datetime->format("Y-m-d"); //2015-03-18 
Problemi correlati