Subtract days to current date with PHP [duplicate]

2

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

$ calc = $ currentTime - $ dataSubtraction; // As a result I wanted it to return number 2 (type, 2 days difference)

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

    
asked by anonymous 20.02.2016 / 23:52

1 answer

1

You can use the date_sub function as per the documentation.

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 by Tue, 19 Jan 2038 03:14:07 GMT. (These are dates that match the maximum and minimum values for a signed 32-bit integer.)

    
21.02.2016 / 02:21