李锋镝的博客

  • 首页
  • 时间轴
  • 说说
  • 左邻右舍
  • 博友圈
  • 关于我
    • 关于我
    • 另一个网站
    • 我的导航站
    • 网站地图
    • 赞助
  • 留言
  • 走心评论
  • 系列文章
  • Now
  • 每日心情
  • 🚇开往
Destiny
自是人生长恨水长东
  1. 首页
  2. 其他
  3. 正文

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

2025年5月6日 约 858 字3 分钟 27点热度 1人点赞 0条评论
本文最后更新于 2025年11月21日,距今已 260 天,其中的信息可能已经发生变化,请注意甄别。

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

主要功能是通过短码显示今天、本周、本月、今年已经过去了多久,当然也可以通过短码指定显示的类型,类型分别定义为:'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库,遂作罢。

菜鸡是这样的。

除非注明,否则均为李锋镝的博客原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.lifengdi.com/others/4394

推荐阅读

  • WordPress实现用户评论等级排行榜插件
  • Kratos+主题新功能预览及功能演示
  • 做了一个WordPress文章热力图插件
  • Dylan Custom Plugin 1.0.3版本更新说明
  • Dylan Custom Plugin 1.0.2版本更新说明
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: PHP WordPress 倒计时 插件 日期 进度条
最后更新:2025年11月21日

岁月同一天 8 月 9 日

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

  • 4 年前 2022年8月9日
    RocketMQ的push消费方式实现详解

    MQ消费方式 消费方式就是指消费者如何从MQ中获取到消息,分为两种方式,push(推方式)和pull(拉方式)。 1、p…

相关文章
  • PHP版本怎么更新啊……2021年4月29日
  • Maven发布本地项目的jar包和源码到私有仓库(maven-source-plugin的简单使用)2020年5月25日
  • 增加了两套复古皮肤-牛皮纸、千禧网页2026年7月15日
  • wordpress增加说说功能2025年4月2日
  • WordPress评论添加UserAgent以及地理位置信息2025年12月1日

李锋镝

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

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

文章评论

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

文章合为时而著,歌诗合为事而作。

听点儿音乐吧 朋友~
文章目录
最新 热点 随机
最新 热点 随机
Kratos+ v1.1.14版本更新说明 Spring Boot 指定外部配置文件的方式 Spring Boot 配置加载优先级总结 Claude Fable 5(claude-fable-5)深度详解 如何通过命令查看Java应用内存中对象数量 关于服务的探活端口和业务端口不一致有什么问题
给主题增加了Now、每日心情、年度回顾、岁月同一天、随机漫步等功能AI时代,个人技术博客的出路在哪里?增加了两套复古皮肤-牛皮纸、千禧网页这个域名注册整整十年了,十年时间,真快啊WordPress实现用户评论等级排行榜插件WordPress网站换了个字体,差点儿把样式换崩了
使用RocketMQ时,服务启动过程中,Consumer在服务未启动时消费消息问题处理 JVM详细参数说明 Kratos+ —— Kratos 主题二次开发记录 MySQL语句执行顺序 一款开源的社交分享插件——share.js 笑死、腹肌……根本不可能有腹肌的~~
最近评论
老张博客 发布于 16 小时前(08月08日) 做的越来越精美了,好看。
Huo 发布于 1 天前(08月07日) 挺有特色的主题,还是感觉 WP 的确是强大
李锋镝 发布于 2 天前(08月07日) 没理解你想说啥
aboss 发布于 2 天前(08月07日) 你的后台web-login?
李锋镝 发布于 2 天前(08月07日) 这个专门的插件实现的功能更好更全,还能对接支付之类的
标签聚合
数据库 WordPress docker Claude JVM 分布式 日常 ElasticSearch K8s AI SpringBoot 架构 AI编程 Spring MySQL SQL JAVA 多线程 IDEA Redis
友情链接
  • 志文工作室
  • 皮皮社
  • Mr.Sun的博客
  • 风渡言
  • 韩小韩博客
  • 搬砖日记
  • 临窗旋墨
  • 知向前端
  • 哥斯拉
  • Blogs·CN
  • 瓦匠个人小站
  • 旧时繁华
  • 彬红茶日记
  • Honesty
  • 韩情脉脉
  • 林羽凡
  • 老张博客
  • 拾趣博客导航
  • 懋和道人

COPYRIGHT © 2026 lifengdi.com. ALL RIGHTS RESERVED.

正在博友圈履约中

域名年龄

Theme Kratos+ By Dylan Li

津ICP备2024022503号-3

京公网安备11011502039375号