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?
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?
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
.
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"
Assuming you are using MySQL, try directly on the SQL expression:
SELECT CAST(seu_campo AS DATE) - INTERVAL 1 DAY;
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"