2010-03-24 17 views

Respuesta

20

strtotime es su amigo

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

Tienes strotime errata para strtotime .. :) demasiado temprano hace solo 5 años ...: D –

+0

@Yegya Ha! Corregido: P –

6

No es el siguiente ejemplo en 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"; 
?> 

Cambiar + a - en la primera (o última) línea obtendrá lo que desee.

-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. 
?> 

También puede usar la función de cadena a la hora para convertir la fecha a la marca de tiempo. como

strtotime(23-09-2013); 
+1

@AndrewBarber La respuesta puede haber sido editada, pero esta respuesta no parece incluir un enlace a nada. – starbeamrainbowlabs

3

De PHP 5.2 puede utilizar DateTime:

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

En lugar de crear DateTime con cadena, puede setTimestamp directamente en objeto:

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