Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!