美文网首页thinkphp@IT·互联网程序员
php创建倒计时工具,让你保持专注

php创建倒计时工具,让你保持专注

作者: 闲睡猫 | 来源:发表于2017-05-24 15:26 被阅读51次

    工作过程中,时常会被各种杂事打乱,有一个倒计时工具可以帮助自己在一定时间内集中注意力。网上虽然有现成的工具,但用着不太顺手,要么功能太简单,要么太复杂,过于占用资源,且缺乏自定义。

    作为爱折腾的程序员,自己用PHP写了一个倒计时工具。

    效果显示

    image.png

    每秒钟更新文件内容,看起来就是倒计时的效果

    image.png

    时间结束后会激活windows弹出窗口:

    image.png

    实现原理

    php程序在后台静默运行,每秒钟执行一次循环并写入到文件,实现倒计时效果。当时间结束后,调用bat文件弹出窗口

    php代码:

    <?php
    $task = '写文章'; // 任务名称
    $duration = 25; // 设置时长
    $rest = 5; // 休息时间
    $now = time();
    $batName = 'clock.bat'; // 调用的bat文件
    $relatePath = './' . $batName;
    $recordPath = './record_time.md'; // 文本倒计时
    $absolutePath = str_replace('php', 'bat', __FILE__);
    $seconds = $duration * 60;
    file_put_contents($recordPath, '');
    for ($i=$seconds; $i > 0; $i--) {
        $hour = floor($i / 3600) ? floor($i / 3600) . '时' : '';
        $minute = floor($i / 60) ? floor($i / 60) . '分' : '';
        $second = $i % 60 ? $i % 60 . '秒' : '';
        $message = "当前任务:{$task}\n当前时间:" . date('Y-m-d H:i:s', time()) . "\n" . "专注时间:{$duration}分钟\n" . "剩下:" . $hour . $minute . $second;
        file_put_contents($recordPath, $message);
        sleep(1);
        if ($i == 1) {
            $content = "Great! You have been working for $duration minute! Now, Relax yourself $rest minute!";
            $content = 'msg * "' . $content . '"';
            file_put_contents($relatePath, $content);
            exec($absolutePath); // 执行bat文件
        }
    }
    

    bat文件:

    msg * "Great! You have been working for 25 minute! Now, Relax yourself 5 minute!"
    

    提示

    image.png
    • 默认情况下,exec函数可能被禁用,要在php.ini中开启该功能

    源码下载

    源码资源的git地址

    相关文章

      网友评论

        本文标题:php创建倒计时工具,让你保持专注

        本文链接:https://www.haomeiwen.com/subject/crkoxxtx.html