李锋镝的博客

  • 首页
  • 时间轴
  • 说说
  • 每日心情
  • Now
  • 系列文章
  • 论坛
  • 左邻右舍
    • 左邻右舍
    • 博友圈
  • 留言
    • 留言
    • 走心评论
  • 关于
    • 关于本站
    • 网站地图
    • 网站统计
    • 另一个网站
    • 我的导航站
    • 赞助
  • 🚇开往
!Destiny
惟坚韧者始能遂其志
  1. 首页
  2. 其他
  3. 正文

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

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

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

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

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

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

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

微信扫一扫分享

标签: PHP WordPress 倒计时 插件 日期 进度条
最后更新:2025年11月21日
相关文章
  • Kratos+ v1.1.14版本更新说明2026年8月7日
  • 本来想把主题上传到WordPress官方主题商店的,结果懵逼了……2026年8月27日
  • WordPress评论添加UserAgent以及地理位置信息2025年12月1日
  • 给主题增加了Now、每日心情、年度回顾、岁月同一天、随机漫步等功能2026年7月21日
  • Kratos+ —— Kratos 主题二次开发记录2026年6月12日

李锋镝

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

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

文章评论

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

惟坚韧者始能遂其志。

听点儿音乐吧 朋友~
文章目录
最新 热点 随机
最新 热点 随机
支撑全网40%网站的WordPress正在重新拥抱PHP生态 Kratos-plus v1.1.24版本更新说明 关于主题加载速度优化的一点儿小演进 市场主流AI编程大模型横向深度分析(2026-09-11) 一款节省token的利器:RTK(Rust Token Killer) RAG太难学?LLM Wiki了解一下
给主题增加了Now、每日心情、年度回顾、岁月同一天、随机漫步等功能WordPress缓存插件WP Fastest Cache、WP Rocket 、FlyingPress对比关于使用AI的一些思考Kratos+ v1.1.16版本更新说明AI时代,个人技术博客的出路在哪里?推荐一个SVG 矢量小图标免费下载网站
Springboot接入DeepSeek API 详解 ZooKeeper 数据持久化 重构 Controller 终极指南:从臃肿到优雅的 7 大黄金法则 + 实战技巧 UUID太长怎么办?快来试试NanoId 写了个日期进度条的小插件 JMX监控权限认证配置
最近评论
blank
lijie blog 发布于 9 小时前(09月14日) 友链申请 名称:lijie blog 链接:https://lijie.cool 描述:懒于当...
blank
Hary 发布于 11 小时前(09月14日) 等PHP9推倒重来,出个船新版本
blank
李锋镝 发布于 23 小时前(09月14日) 不愧是皮总,这措辞~ :30:
blank
皮皮社长 发布于 23 小时前(09月14日) :20: 我勒了个去,中国汉字波大精深。 :42:
blank
李锋镝 发布于 23 小时前(09月14日) 我这里面也带了一堆小表情包,看看不行也优化一波
标签聚合
Redis WordPress AI SpringBoot Claude 架构 JAVA 分布式 AI编程 K8s ElasticSearch Spring 多线程 日常 IDEA MySQL MQ JVM SQL 数据库
友情链接
  • 若梦博客
  • 九仞之行
  • sssr7844的博客
  • Honesty
  • 彬红茶日记
  • 蜗牛工作室
  • 老张博客
  • 搬砖日记
  • 瓦匠个人小站
  • 志文工作室
  • 韩情脉脉
  • 知向前端
  • Serendipity
  • 临窗旋墨
  • 韩小韩博客
  • 皮皮社
  • 哥斯拉
  • Mr.Sun的博客
  • 懋和道人
  • 林羽凡

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

Domain age badge for lifengdi.com

Theme Kratos-plus By Dylan Li

津ICP备2024022503号-3

京公网安备11011502039375号