Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda