php - 如何在没有 Crontab 的情况下每 2 周在 PHP 中调用一个方法

标签 php

我想更新 html 中的 div,以便每周仅使用 php 而不使用 cron 来显示不同的内容。 怎么可能实现它?以下代码为每2个月第10天激活码 但我想逻辑会有所不同。如果可能的话,一些提示或例子会很棒!我很乐意听取您的意见!

    // 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 */

    }
}

最佳答案

我刚刚更新了您的示例,现在它将在每个下周一运行:

$weekInYearNumber = (int)date('W');
$weekDayNumber = (int)date('w');
if ($weekDayNumber === 1 && $weekInYearNumber % 2 == 0) {
    // Rest of your code.
}

关于php - 如何在没有 Crontab 的情况下每 2 周在 PHP 中调用一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43671971/

相关文章:

php - 在 curl/php 中保持事件状态

php - 我有一个可用的 Laravel 选择查询。我如何添加过滤器?

php - 如何使用来自两个模型的数据生成 yii2 表单,但需要将更改保存到其中一个

php - 将 laravel 错误消息更改为 JSON for API

php - CakePHP 性能问题 - 30-50 秒的模式加载

php - 从 MySQL 数据库填充选择字段表单数据并选择正确的值

php - 如何在 PHP 中从 MySQL 创建自定义 JSON 布局

php - Javascript:等效于 PHP 的 hash_hmac() 与 RAW BINARY 输出?

php - 获取列年份的总和

php - 图片上传无效