PHP - Take a value and modify the date day

Asked

Viewed 125 times

2

I have a question about date manipulation. I need to take 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 looking to do is, a routine expense, so the user informs me a fixed spending day, (Every day 30 I will spend R$50) and the system every day 30 will subtract R$50 from the account. What begets me another doubt...

After the date manipulation how to do this date checking? (If today is day 30 or not // To know when to subtract the R $ 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

Scheduling Of Tasks

  • I used the Taskscheduler windows.
  • I asked the Task to open every day PHP.exe. //have to open php.exe pq otherwise the task will open the page and in my case I wanted to run the script
  • And how to open the directory of my script (c:xampp htdocs test.php).
  • 1

    Welcome to Stackoverflow in English. I edited your question to remove the greetings as we usually keep the text as clean as possible to focus on your scheduling question. If you are interested in visiting a part of the site that is not aimed to ask questions can know the [chat]. If you have questions about the operation, rules and procedures of the site visit the [meta] :)

1 answer

2


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 chose exists in the month. For example, in the month of February the maximum day may be 28 or 29. Then 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 linux task to run this script daily, you can use crontab.

There are two very good publications here:

How to schedule a recurring task on linux?

Set up Cronjob to run every 5 minutes, when it’s between 5 to 20 hours

There is also the possibility to perform events directly in the database. There is a great post about it here:

Scheduled tasks in PHP (the answer is in mysql)

  • This is Andrei !! So face it solves my problem with date manipulation, however IF does not solve my scheduling problem, because I need this code run every day, (not say the whole day, but at a specific time) to validate whether to subtract the value or not

  • For this you need to create a Crontab. It will run this script every day and do the check...

  • @Pedroterencio-Gamerartist veja: https://answall.com/questions/2818/comor-agendar-uma-tarefa-recorrente-no-linux/2827#2827

  • Cool, but in my case, I’m running on a localhost, and my OS is a windows 10, crontab would work the same way ?

  • @Pedroterencio-Gamerartist you’re using a shaman or wampp right?

  • This, using shampoo, with PHP5

  • Sensational, I will test here, and if everything goes well, edit the post with the solution

  • @Pedroterencio-Gamerartist if my answer is right, please qualify it. Hug!

Show 3 more comments

Browser other questions tagged

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