李锋镝的博客

  • 首页
  • 时间轴
  • 说说
  • 读书观影
  • 系列
  • AI工具集
  • 论坛
  • 友圈
    • 友链
    • 朋友圈
    • 游客中心
  • 留言
    • 留言
    • 走心评论
  • 关于
    • 关于本站
    • Now
    • 每日心情
    • 网站地图
    • 网站统计
    • 赞助
  • 星球穿梭
  • 🚇开往
静亦定 动亦定
惟坚韧者始能遂其志
  1. 首页
  2. 其他
  3. 正文

写了个日期进度条的小插件

2025年5月6日 260 1 0
本文最后更新于 2025年11月21日,距今已 351 天,其中的信息可能已经发生变化,请注意甄别。

今天逛博客,看到其他大佬的博客有个时间进度条的小插件,意动之下,就决定自己也搞一个玩玩。

主要功能是通过短码显示今天、本周、本月、今年已经过去了多久,当然也可以通过短码指定显示的类型,类型分别定义为:'day' - 今天, 'week' - 本周,'month' - 本月, 'year' - 今年。

使用方式很简单,示例如下:

  • [ countdown ]:不指定类型,全部展示。
  • [ countdown type="day" ]:只展示当天的倒计时。
  • [ countdown type="day,week" ]:展示当天和本周的倒计时。

插件整体颜色使用了莫兰迪色系,当然大家也可以根据自己的喜好进行调整。

file

整个插件代码如下,需要的自取:

<?php

// 定义短码处理函数
function countdown_shortcode( $atts ) {
    $default_atts = array(
        'type' => 'all', // 默认全部展示
        'title' => ''
    );
    $atts = shortcode_atts( $default_atts, $atts );

    $types = explode(',', str_replace(' ', '', $atts['type']));
    if (in_array('all', $types)) {
        $types = ['day', 'week','month', 'year'];
    }

    $valid_types = ['day', 'week','month', 'year'];
    $color_map = [
        'day' => '#B5EAD7',
        'week' => '#FFDAC1',
        'month' => '#E2F0CB',
        'year' => '#FFB7B2'
    ];

    $style = '<style>
       .countdown-container {
            background-color: #FAF9F6;
            border-radius: 12px;
            padding: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.08);
        }
       .countdown-item {
            height: 50px;
            position: relative;
            max-height: 80px;
            overflow: hidden;
        }
       .countdown-text {
            color: #868e96;
            font-size: 0.95em;
        }
       .countdown-progress-bar {
            height: 16px;
            background-color: #E8E8E8;
            border-radius: 8px;
            overflow: hidden;
            position: relative;
            margin-top: 3px;
        }
       .progress-bar-fill {
            height: 100%;
            border-radius: 8px;
            transition: width 1s ease;
            position: relative;
            background-image: repeating-linear-gradient(
                -45deg,
                rgba(255, 255, 255, 0.2),
                rgba(255, 255, 255, 0.2) 10px,
                transparent 10px,
                transparent 20px
            );
            background-size: 28px 28px;
            animation: spiral-move 1s linear infinite;
        }
       .countdown-percentage {
            font-size: 0.8em;
            color: #fff;
            position: absolute;
            top: 50%;
            right: 5px;
            transform: translateY(-50%);
            z-index: 2;
        }

        @keyframes spiral-move {
            0% {
                background-position: 0 0;
            }
            100% {
                background-position: 28px 0;
            }
        }
    </style>';

    $script = '<script>
        function updateCountdown() {
            const now = new Date();
            const types = '. json_encode($types). ';
            const colorMap = '. json_encode($color_map). ';
            const validTypes = '. json_encode($valid_types). ';

            types.forEach(type => {
                if (!validTypes.includes(type)) return;

                let elapsed = 0;
                let total = 0;
                let text = "";

                switch (type) {
                    case "day":
                        elapsed = now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds();
                        total = 24 * 3600;
                        text = `今天已过去 ${Math.floor(elapsed / 3600)} 小时 ${Math.floor((elapsed % 3600) / 60)} 分钟`;
                        break;
                    case "week":
                        elapsed = (now.getDay() * 24 * 3600) + (now.getHours() * 3600) + (now.getMinutes() * 60) + now.getSeconds();
                        total = 7 * 24 * 3600;
                        text = `本周已过去 ${Math.floor(elapsed / (24 * 3600))} 天 ${Math.floor((elapsed % (24 * 3600)) / 3600)} 小时 ${Math.floor((elapsed % 3600) / 60)} 分钟`;
                        break;
                    case "month":
                        const year = now.getFullYear();
                        const month = now.getMonth() + 1;
                        const daysInMonth = new Date(year, month, 0).getDate();
                        elapsed = ((now.getDate() - 1) * 24 * 3600) + (now.getHours() * 3600) + (now.getMinutes() * 60) + now.getSeconds();
                        total = daysInMonth * 24 * 3600;
                        text = `本月已过去 ${Math.floor(elapsed / (24 * 3600))} 天 ${Math.floor((elapsed % (24 * 3600)) / 3600)} 小时 ${Math.floor((elapsed % 3600) / 60)} 分钟`;
                        break;
                    case "year":
                        const startOfYear = new Date(now.getFullYear(), 0, 1);
                        const diff = now - startOfYear;
                        elapsed = Math.floor(diff / 1000);
                        const isLeapYear = (new Date(now.getFullYear(), 1, 29).getDate() === 29);
                        total = (isLeapYear? 366 : 365) * 24 * 3600;
                        text = `今年已过去 ${Math.floor(elapsed / (24 * 3600))} 天 ${Math.floor((elapsed % (24 * 3600)) / 3600)} 小时 ${Math.floor((elapsed % 3600) / 60)} 分钟`;
                        break;
                }

                const percentage = Math.round((elapsed / total) * 100);
                const item = document.getElementById(`countdown-${type}`);
                if (item) {
                    const textElement = item.querySelector(".countdown-text");
                    const percentageElement = item.querySelector(".countdown-percentage");
                    const progressBarElement = item.querySelector(".progress-bar-fill");

                    textElement.textContent = text;
                    percentageElement.textContent = `${percentage}%`;
                    progressBarElement.style.width = `${percentage}%`;
                    progressBarElement.style.backgroundColor = colorMap[type];
                }
            });
        }

        document.addEventListener("DOMContentLoaded", () => {
            updateCountdown();
            setInterval(updateCountdown, 1000 * 60);
        });
    </script>';

    ob_start();
    echo $style;
    echo '<div class="countdown-container">';
    foreach ($types as $type) {
        if (!in_array($type, $valid_types)) continue;
        ?>
        <div class="countdown-item" id="countdown-<?php echo $type;?>">
            <span class="countdown-text"></span>
            <div class="countdown-progress-bar">
                <div class="progress-bar-fill">
                    <span class="countdown-percentage"></span>
                </div>
            </div>
        </div>
        <?php
    }
    echo '</div>';
    echo $script;
    return ob_get_clean();
}

add_shortcode('countdown', 'countdown_shortcode');

本来想搞一个最近的节假日的倒计时的,不过没有找到合适的JS库,遂作罢。

菜鸡是这样的。

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
分享到

写了个日期进度条的小插件

也可使用浏览器菜单中的「分享」功能

微信扫一扫分享

标签: PHP WordPress 倒计时 插件 日期 进度条
最后更新:2025年11月21日

岁月同一天 11 月 7 日

回望过去的今天,你在写什么

  • AI
    1 年前 2025年11月7日
    DeepSeek本地部署全攻略:从环境搭建到高级应用,打造专属 AI 助手

    DeepSeek 作为国产开源大模型的佼佼者,凭借出色的推理能力、代码生成效率和中文支持,成为很多开发者本地部署的首选。…

相关文章
  • WordPress评论添加UserAgent以及地理位置信息2025年12月1日
  • Kratos+ v1.1.18版本更新说明2026年8月19日
  • wordpress增加说说功能2025年4月2日
  • 让WordPress静态化之Rocket‑Nginx2026年9月18日
  • 支撑全网40%网站的WordPress正在重新拥抱PHP生态2026年9月14日

李锋镝

既然选择了远方,便只顾风雨兼程。

打赏 点赞
< 上一篇
下一篇 >
1234567891112131415161718192021222324252627282930313233343536373839404142434446474849505152535455575859606162636465666769727476777879808182858687909293949596979899
取消回复
…

文章评论

还没有评论,快来抢沙发吧~

... the truly free individual is free only to the extent of his own self-mastery. While those who will not govern themselves are condemned to find masters to govern over them.

听点儿音乐吧 朋友~
文章目录
最新 热点 随机
最新 热点 随机
给服务器加了WAF 又一个逆天改命的人:小小徐 AI的生成能力等于生产力吗? NGINX严重漏洞(CVE-2026-15176、CVE-2026-93479、CVE-2026-97205) Kratos-plus v1.1.27更新说明 php2shell远程执行漏洞修复(CVE-2026-109618 和 CVE-2026-109619)
给主题增加了游客中心功能心气没了,干什么都不得劲我觉得是时候公布我的导航网站了给服务器加了WAF游本昌去世关于主题加载速度优化的一点儿小演进
中国文学史上最霸气的诗句是哪一首?这首诗当仁不让 JAVA关键字之volatile关键字说明 SVN服务端和客户端的安装以及在MyEclipse中的配置 Spring事件驱动深度指南:从单机异步到亿级流量,比MQ更轻的架构神器 如何提升微服务的幸福感 JDK25模块级导入深度解析:Java导入机制的革命性进化
最近评论
fairy 发布于 21 小时前(11月06日) 机器人快来我这里吧!
李锋镝 发布于 21 小时前(11月06日) 都是机器人,防不住
李锋镝 发布于 21 小时前(11月06日) 是的,太烦了
李锋镝 发布于 21 小时前(11月06日) 可以同一台服务器
fairy 发布于 23 小时前(11月06日) 这流量,好厉害啊。我一年都没那么多
标签聚合
JVM 分布式 Claude Theme SQL MySQL ElasticSearch JAVA 架构 AI编程 K8s 多线程 IDEA Redis Spring 数据库 日常 WordPress SpringBoot AI
友情链接
  • 林羽凡
  • 知向前端
  • 皮皮社
  • 彬红茶日记
  • 瓦匠个人小站
  • 老张博客
  • 懋和道人
  • sssr7844的博客
  • 刘永博客
  • 搬砖日记
  • 志文工作室
  • Honesty
  • 韩小韩博客
  • lijie blog
  • 韩情脉脉
  • Mr.Sun的博客
  • Serendipity
  • 九仞之行
  • 哥斯拉

COPYRIGHT © 2016-2026 lifengdi.com. ALL RIGHTS RESERVED.

lifengdi.com 十年之约虫洞

Domain age badge for lifengdi.com

Theme Kratos-plus By Dylan Li

津ICP备2024022503号-3

京公网安备11011502039375号