Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
How to call a method every 2 weeks in PHP without a Crontab

I want to update the div in html to display different contents every week with just php and without the use of cron. How will it be posssible to achieve it ? The following code is for activating the code every 2 month on the 10nth day but I guess the logic will be different. If possible some hint or example will be great ! I will love to hear from you!

    // check if current day is 10th and month is an even number
if (date('d')==10 && date('m') % 2 == 0) {
    // get todays-date (format: yyyymmdd)
    $today = date('Ymd'); 
    // get info about last run
    $last_run = variable_get('my_last_run', 0);
    // check last run was not today
    if ($last_run!=$today) {
        // set last run to today
        variable_set('my_last_run', $today);

        /* Place your code here */

    }
}
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I've just updated your example, now it will work every next Monday:

$weekInYearNumber = (int)date('W');
$weekDayNumber = (int)date('w');
if ($weekDayNumber === 1 && $weekInYearNumber % 2 == 0) {
    // Rest of your code.
}
about 4 years ago · Santiago Trujillo Relatório

0

If you want to do it quickly, you can count weeks since the first of january 2018 (because it was monday: any other year started with a monday will be ok), and then check if that number is odd or even:

      $diffBetweenNowAndFirstJan2018 = time() - strtotime("2018-01-01");
      $numberOfWeeksSinceFirstJan2018 = floor($diffBetweenNowAndFirstJan2018 / (60 * 60 * 24 * 7));
      $isNumberOfWeeksOdd = ($numberOfWeeksSinceFirstJan2018 % 2 == 1)

      if($isNumberOfWeeksOdd){
            // it's other week time
    }

In one line:

     if( ( (floor((time() - strtotime("2018-01-01")) / (60 * 60 * 24 * 7))) % 2 == 1) ){
            // it's other week time
    }

You should not use date('W') as Vladmir says, because between new year's eve you could get two "odd" week consecutively.

about 4 years ago · Santiago Trujillo Relatório

0

If you can't or don't want to use a cron job for that (which would be the easiest and recommended solution), you can create a PHP script that spends most of the time sleeping, and wakes up to check the date and run whatever you need.

If you don't care about dates and just want to run it every two weeks, you can make it sleep like this:

 sleep(14*24*60*60);

Have your script execute your code after that, and put all that inside an infinite loop. Then just run the script from the console so it doesn't have an execution time limit.

Take into account that the script may stop running for different reasons, for example when servers reboot, so you should monitor the process and re-run it automatically if it's not running or on reboot.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda