Convert String to Date and remove 1 day from String

Asked

Viewed 120 times

2

I got this string:

$data_final = "26/11/2017";

And I need this variable to look like 25/11/2017. That is, I need to convert string to date and remove 1 day from that date.

  • 2

    echo date('d/m/Y', strtotime('-1 days', strtotime('26-11-2017')));

  • This code did not work, it showed 31/12/1969

  • 1

    @Felipemorenoborges take a look at my answer. You try trying with the date in Brazilian or American format?

  • 1

    @Felipemorenoborges Rsantos' answer is now right. It is worth taking a look too.

  • @Did Felipemorenoborges solve your question? If yes, mark it as correct to facilitate others who may have the same question to find the answer more easily.

3 answers

4

$data = '26/11/2017';

$data = DateTime::createFromFormat('d/m/Y', $data);
$data->sub(new DateInterval('P1D')); // -1 dia
echo $data->format('d/m/Y');

I used the sub to remove the amount of days required, content HERE

Briefly the P symbolizes the period, the 1 the amount of the period to be removed and the D symbolizes days.

  • This code did not work, it showed 31/12/1969

  • 1

    His answer has a little problem: AP asked in the format d/m/Y, and not in the American format. This must have been the error.

  • @R.Santos just edit it and fix it. So the negatives can be removed and the answer will help AP. This would be a good option: https://www.w3schools.com/PHP/func_date_create_from_format.asp

  • @Wallacemaxters adjusted the answer according to other content that found what you think?

  • @R.Santos looked great. I tested it here and it worked perfectly. I’ll take off the -1 and give you an upvote.

  • 1

    @Wallacemaxters Oh that great then :)

Show 1 more comment

3


Use the method DateTime::createFromFormat. Through this method it is possible to create an object DateTime from any format. Then having the created object we use the method modify, which accepts as a parameter the same values as strtotime. In the end, we use the method format to get the date in the desired format.

Behold:

$data_final = '26/11/2017';

$ontem = DateTime::createFromFormat('d/m/Y', $data_final)->modify('-1 day');

echo $ontem->format('d/m/Y');

See an example running on Ideone

  • It worked, thank you very much!

  • 2

    @Felipemorenoborges Good, man! consider marking as "I accept" the answer that solved your problem

  • 1

    @Felipemorenoborges if the answer solved your problem please mark it as correct. If you do not know how to do this read: https://pt.meta.stackoverflow.com/q/1078/3635

-1

If you ensure that the variable will always come in xx/xx/xxxx format you can simply use explode followed by implode.

$data_final = explode("/","26/11/2017");

var_dump($data_final);

$data_final[0]--;

echo $data_final[0];

$data_final = implode("/",$data_final);

var_dump($data_final);

But I advise you to use the solution given by Comrade R.Santos

echo date('d/m/Y', strtotime('-1 days', strtotime('26-11-2017')));
  • Same problem as the previous answer. AP requested in format d/m/Y (Brazilian) and not in American format. Therefore, your answer does not apply to the question.

Browser other questions tagged

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