How do I reset the time and decrease one day of a DATETIME?

5

I have a field DATETIME that I get from SQL, format YYYY-mm-dd H-m-s and I need to zero the time and decrease one day.

I'm getting a day off by using date_modify($date, '-1 day'); , but how do I reset the time?

    
asked by anonymous 21.03.2014 / 14:45

4 answers

1

Try the following code:

$date = date_create('2000-01-20 01:00:00');
date_sub($date, date_interval_create_from_date_string('1 days'));
echo date_format($date, 'Y-m-d');

Response based on the first example documentation for DateTime::sub -- date_sub .

    
21.03.2014 / 14:49
2

This will solve it:

<?php echo date("Y-m-d", strtotime("2014-03-27" . " - 1 days")); ?>

Being that "2014-03-27" is your specific date.

You can set the static time on your custom date to "zero": "2014-03-27 00:00:00"

    
21.03.2014 / 14:52
0

Assuming you are using MySQL, try directly on the SQL expression:

SELECT CAST(seu_campo AS DATE) - INTERVAL 1 DAY;
    
21.03.2014 / 15:02
0

I ended up doing this:

//Gerando um datetime $data novo para nao ter que colocar a parte do SQL
$data = new DateTime('2014-03-21 23:50:10');
$dia_anterior_hora_zerada = new DateTime();
$dia_anterior_hora_zerada = $data;
$dia_anterior_hora_zerada->setTime(00,00,00);
$dia_anterior_hora_zerada->modify('-1 day');
//$dia_anterior_hora_zerada = "2014-03-20 00:00:00"
    
21.03.2014 / 15:41