Subtract days from current date with PHP

Asked

Viewed 2,185 times

2

Hello, I would like to know how to decrease, for example like this: $datacurrent = '20/02/2016'; $dataSubtracao = '18/02/2016';

$Calc = $dataAtual - $dataSubtracao; //As a result I wanted to return the number 2 (type, 2 days difference)

I know this example will go wrong, but I want the result to return the entire value of the difference between the two dates.

1 answer

1

You can use the function date_sub as documented.

Another option is the implementation by converting the dates to seconds, subtracting the seconds and then converting the result to the date type again.

$today = date('Y-m-d'); //recebe a data atual

$seconds = strtotime($today); //converte para segundos

$diff_date = date("Y-m-d",($seconds - 86400)); //subtrai um dia (valor em segundos) e converte para um objeto do tipo data

Note: The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are dates that correspond to the maximum and minimum values for a signed 32-bit integer.)

Browser other questions tagged

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