Subtract dates in PHP [duplicate]

1

I'm having a project where I need to change information from week to week.

Is there an automated PHP function that can help me subtract today's date by a base date I defined. For example:

$datadehoje = (int)date("d/m/Y");
$database = (int)date("15/07/2018");
$resultado = $datadehoje - $database;

The above code is spelled wrong but is to more or less exemplify what I wanted. The project will provide information for months and do not want to have to create an algorithm for it.

    
asked by anonymous 04.08.2018 / 11:51

2 answers

3

You can not convert a date to int and get something meaningful. You have to make a difference by using the date type itself to get a meaningful result, which will be a date range. Then you can get the amount of days it is a text, if you want you can convert that text to number if you are going to do it.

$database = date_create('2018-07-15');
$datadehoje = date_create();
$resultado = date_diff($database, $datadehoje);
echo date_interval_format($resultado, '%a');
04.08.2018 / 12:44
1

One of the ways to do an operation with dates in PHP is:

// a partir da data de hoje
echo date("d/m/Y",strtotime(date("Y-m-d")."+12 month"));

// a partir de outras datas
echo date("d/m/Y",strtotime(date("Y-m-d",strtotime($data_de_referencia))."-12 month"));
  

Reference date ()

     

Reference strtotime ()

    
04.08.2018 / 12:44