PHP - Take a value and modify the day of the date

1

Good morning guys, I'm having a question about date manipulation. I need to get a value (given by the user) and turn this value into day of the month.

Example:

$Valor_Usuario = 30;
$Data = 12/10/2018;
$Resultado = 30/10/2018;

What I'm trying to do is, a spending routine, so the user informs me a fixed day of spending, (Every day 30 I'll spend $ 50) and the system every 30 days will subtract $ 50 from the account. What gives me another doubt ...

After manipulating the date how do this checking the date? (If today is day 30 or not // To know when to subtract the $ 50)

RESOLVED

Date manipulation:

$data = explode("-",date('d-m-Y')); //coloquei a data em array
$data[0] = 31; //mudei "manualmente" o array do dia ($data[0])
$nova = implode("-", $data); //juntei os arrays novamente
NA TELA = 31-12-2018
$check = date("t", strtotime('m-Y')); //Peguei o dia max do mes Ex:28,29...
$conv = $data[0] < $check ? $data[0]:$check; //Se a DT do usuario for < DT do mes = DT do usuario SENAO DT do mes

Task Scheduling

  • I used the TaskScheduler of windows. Home
  • I asked the Task to open PHP.exe every day. // have to open php.exe because only the task will open the page and in my case I wanted to run the script
  • And as a parameter open the directory of my script (c: xampp \ htdocs \ test \ test.php).
asked by anonymous 15.12.2018 / 17:31

1 answer

1

A simple date() should solve your problem:

$hoje = date("d/m/Y");
$diaDoUsuario = 30;
$proximaData = $diaDoUsuario."/".date("m/Y");

if($proximaData == $hoje){
    echo "SIM! É hoje que vou subtrair o valor!";
}

However, you may need to know if the day he has chosen exists in the month. For example, in February the maximum day can be 28 or 29. So I would do so:

$hoje = date("d/m/Y");
$diaDoUsuario = 30;
// PEGA O DIA MÁXIMO DO MÊS
$diaMaximo = date("t", strtotime(date("Y-m")));
// SE O DIA DO USUÁRIO FOR MAIOR QUE O DIA MÁXIMO DO MÊS, ENTÃO O DIA MÁXIMO SERÁ ESCOLHIDO, SE NÃO, DIA DO USUÁRIO
$diaCorreto = $diaDoUsuario > $diaMaximo ? $diaMaximo : $diaDoUsuario;

$proximaData = $diaCorreto."/".date("m/Y");

if($proximaData == $hoje){
    echo "SIM! É hoje que vou subtrair o valor!";
}

To schedule a task in linux to run this script daily, you can use crontab.

You have two very good posts here:

How to schedule a recurring task in linux?

Set up Cronjob to run every 5 to 5 minutes when it is between 5 to 20 hours

There is also the possibility of executing events directly in the database. You have a great post on this here:

Scheduled Tasks in PHP (the answer is in mysql)

    
15.12.2018 / 17:49