How to reset the hour and decrease a day of a DATETIME?

Asked

Viewed 1,002 times

5

I have a field DATETIME I take from SQL, format YYYY-mm-dd H-m-s and I need to reset the hour and shorten a day.

I’m managing to decrease a day using date_modify($date, '-1 day');, but how to reset the hour?

  • has 3 great answer and I don’t believe any of them served you, please if you find any good answer below mark it as right or give an upvote :) Thank you

  • Right. I ended up doing it another way, I voted the one that helped me the most. I’ll post my solution, maybe help someone.

  • Okay, thank you :)

  • @Even Magistermundus you can accept your own answer, if it was the one that helped the most.

4 answers

2

That takes care of:

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

"2014-03-27" being its specific date.

You can put static time on your custom "reset" date: "2014-03-27 00:00:00"

  • Yes, but I believe he said decrease a day and not increase a day, but otherwise good answer.

  • I confused the signal, I’m already editing my answer for better understanding.

1


0

Assuming you are using Mysql, try directly in the SQL expression:

SELECT CAST(seu_campo AS DATE) - INTERVAL 1 DAY;

0

I ended up doing so:

//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"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.