Difference between two dates in PHP in months of 31, 28 and 29 days

2

I have a bonus system that only frees the bonus after 30 exact days of the user's registration. To compare the current date with the user's registration date so that I get how many days left to complete 30 exact days, I use the following code PHP :

$time1 = new DateTime($user->date); //data do cadastro do usuário (MySQL timestamp)
$time2 = new DateTime(date("Y-m-d H:i:s")); //data atual
$interval =  $time2->diff($time1);
$faltam_dias = 30 - $interval->d;

Doubt:

I took this test in the current month (June) that contains exactly 30 days. If the current month is 31 or 28/29 (February) days, how would you get the correct number of days to complete the 30 days of the bonus?

    
asked by anonymous 03.06.2015 / 03:57

2 answers

5

Use the days property instead of d :

$time1 = new DateTime('20150501'); 
$time2 = new DateTime(); //data atual
$interval =  $time2->diff($time1);
echo $interval->days;

link

    
03.06.2015 / 04:29
-2

Use +1 Months ... instead of 30 days, it might make it much easier to use your code ....

$data_inicial = 2017-07-01;
$data_final = date("Y-m-d", strtotime('+1 Month', $data_inicial));
echo $data_final;

I believe it will be printed:

  

2017-08-01

And I also believe that it will be possible to deliver 12 bonuses exactly per year

:)

    
01.07.2017 / 18:35