侧边栏壁纸
博主昵称
YunZheng

独处未必孤独,喜欢就是自由

时间段模糊一言 API — 让你的应用更有温度

2026年05月01日 12阅读 0评论 1点赞
创创云-专业云计算服务器提供商

✨ 项目简介

时间段模糊一言 是一个纯 PHP 实现的 RESTful API,无需数据库依赖,通过本地时间计算即可返回对应时间段的问候语与暖心文案。无论是早安、午安还是晚安,它都能精准匹配——让你的应用、网站或机器人瞬间拥有"温度"。
time

核心特性

特性说明
零依赖纯 PHP 实现,无需数据库 / Redis / 任何第三方库
毫秒响应本地数组查询 + 随机选取,无网络开销
🆓 完全免费无需注册、无需密钥、无调用频率限制
🌈 双版本V1 固定文案版 / V2 随机丰富版
🌍 跨域支持已配置 CORS,前端可直接调用
🔒 参数校验无效 type 参数返回友好错误提示
💝 7 个时段凌晨/早晨/上午/中午/下午/傍晚/晚上,全覆盖

🚀 快速开始

接口调用

# V1 固定文案版(默认)
curl "http://apis.uctb.cn/api/time?type=1"

# V2 随机丰富版(推荐,文案更多变)
curl "http://apis.uctb.cn/api/time?type=2"

返回示例

V1 版本(固定文案):

{
    "code": 1,
    "msg": "晚上好,好好休息!",
    "nowtime": "2025-09-07 PM 20:40:00",
    "nxyj": "晚上是放松的时刻~感谢今天的努力,明天继续加油!",
    "version": "v1"
}

V2 版本(随机文案):

{
    "code": 1,
    "msg": "晚安,愿你好梦!",
    "nowtime": "2025-09-07 PM 23:15:00",
    "nxyj": "晚安,愿你的梦境如星空般美丽~明天会更好!",
    "version": "v2"
}

错误响应:

{
    "code": 0,
    "msg": "参数错误",
    "error": "type参数只能为1或2",
    "nowtime": "2025-05-01 PM 13:40:00",
    "version": "error"
}

字段说明

字段类型说明
codeint状态码:1 成功,0 参数错误
msgstring时间段问候语
nowtimestring当前服务器时间(Asia/Shanghai 时区)
nxyjstring暖心一句话(V2 为随机选取)
versionstring当前版本标识:v1 / v2 / error

⏰ 时间段划分(基于实际代码)

API 将一天划分为 7 个时间段

凌晨 (00:00 - 04:59)   → 5 小时  → "凌晨了,注意休息哦!"
早晨 (05:00 - 07:59)   → 3 小时  → "早晨好,新的一天开始了!"
上午 (08:00 - 10:59)   → 3 小时  → "上午好,工作学习加油!"
中午 (11:00 - 12:59)   → 2 小时  → "中午了,记得吃午饭哦!"
下午 (13:00 - 16:59)   → 4 小时  → "下午好,保持好状态!"
傍晚 (17:00 - 18:59)   → 2 小时  → "傍晚了,放松一下吧!"
晚上 (19:00 - 23:59)   → 5 小时  → "晚上好,好好休息!"

V1 vs V2 区别

  • V1:每个时段固定 1 条问候语 + 1 条暖心文案,结果确定性强
  • V2:每个时段内置 3~5 条 问候语 + 3~5 条 暖心文案,每次调用随机组合,内容更丰富多样

💻 核心代码解析

技术栈

  • 语言:PHP 7.4+
  • 依赖:零依赖,单个 index.php 文件即可运行
  • 时区Asia/Shanghai
  • 编码:UTF-8,JSON_UNESCAPED_UNICODE 输出

核心逻辑

// 1. 获取当前小时数,判断所属时段
$hour = date('G', $now);

if ($hour >= 0 && $hour < 5) {
    $timeKey = '凌晨';
} elseif ($hour >= 5 && $hour < 8) {
    $timeKey = '早晨';
} elseif ($hour >= 8 && $hour < 11) {
    $timeKey = '上午';
} elseif ($hour >= 11 && $hour < 13) {
    $timeKey = '中午';
} elseif ($hour >= 13 && $hour < 17) {
    $timeKey = '下午';
} elseif ($hour >= 17 && $hour < 19) {
    $timeKey = '傍晚';
} else {
    $timeKey = '晚上';
}

// 2. V2 版本使用 array_rand 随机选取文案
$timeMsgIndex = array_rand($timeMessagesV2[$timeKey]['time_msgs']);
$warmMsgIndex = array_rand($timeMessagesV2[$timeKey]['warm_msgs']);

// 3. 输出 JSON(支持中文不转义)
echo json_encode($response, JSON_UNESCAPED_UNICODE);

数据结构设计

// V2 版本数据结构(每个时段包含 time_msg + warm_msg + 候选数组)
$periodData = [
    'time_msg' => '默认问候语',           // V1 使用此字段
    'warm_msg' => '默认暖心语',           // V1 使用此字段
    'time_msgs' => ['问候1','问候2',...], // V2 从中随机选
    'warm_msgs' => ['暖心1','暖心2',...]  // V2 从中随机选
];

各时段文案数量统计

时段V1 文案V2 候选组合数
凌晨1+13 × 3 = 9 种
早晨1+13 × 3 = 9 种
上午1+15 × 5 = 25 种
中午1+15 × 5 = 25 种
下午1+15 × 5 = 25 种
傍晚1+15 × 5 = 25 种
晚上1+15 × 5 = 25 种

V2 总计可达 143 种不同组合输出!


🛠️ 本地部署

方式一:直接部署

  1. index.php 上传至 Web 服务器目录
  2. 确保 PHP 7.4+ 环境
  3. 直接访问即可使用

方式二:PHP 内置服务器(开发调试)

php -S localhost:8080 index.php

然后访问:http://localhost:8080/?type=2

方式三:Docker 部署

FROM php:8.1-apache
COPY index.php /var/www/html/index.php
EXPOSE 80
docker build -t time-api .
docker run -d -p 8080:80 time-api

💻 调用示例

JavaScript / Node.js

async function getGreeting(type = 2) {
  const res = await fetch(`https://apis.uctb.cn/api/time?type=${type}`);
  const data = await res.json();
  
  if (data.code === 1) {
    console.log(`📢 ${data.msg}`);       // 问候语
    console.log(`🕐 ${data.nowtime}`);    // 当前时间
    console.log(`💬 ${data.nxyj}`);      // 暖心一句
    console.log(`📋 版本: ${data.version}`);
  } else {
    console.error(`❌ ${data.error}`);
  }
  return data;
}

getGreeting(2);

其它调用示例还请查看我的API官网,或者请教AI直接对接

### 🎯 应用场景

场景一:个人主页 / 博客欢迎语

在页面加载时动态展示当前时段的问候:

<div id="welcome"></div>
<script>
fetch('https://apis.uctb.cn/api/time?type=2')
  .then(r => r.json())
  .then(d => {
    document.getElementById('welcome').innerHTML = 
      `<h3>${d.msg}</h3><small>${d.nxyj}</small>`;
  });
</script>

场景二:QQ / 微信机器人定时推送

const schedule = require('node-schedule');

// 每天 7:00 发送早安,22:00 发送晚安
schedule.scheduleJob('0 7 * * *', async () => {
  const { msg, nxyj } = await getGreeting(2);
  sendToGroup(`${msg}\n${nxyj}`);  // 推送到群
});

schedule.scheduleJob('0 22 * * *', async () => {
  const { msg, nxyj } = await getGreeting(2);
  sendToGroup(`${msg}\n${nxyj}`);  // 晚安推送
});

场景三:终端 / 命令行工具启动提示

.bashrc.zshrc 中添加:

greeting() {
  curl -s "https://apis.uctb.cn/api/time?type=2" | \
    python3 -c "import sys,json;d=json.load(sys.stdin);print(f\"\033[36m{d['msg']}\033[0m\n\033[33m{d['nxyj']}\033[0m\")" 2>/dev/null
}
greeting
echo ""

每次打开终端都能看到温暖的问候。

场景四:签到系统 / 打卡应用

作为每日签到页面的动态欢迎语数据源,让用户每天看到不同的鼓励话语。

场景五:桌面应用启动屏

Electron / Tauri 等桌面应用的欢迎界面,展示当前时段问候。

📊 项目数据

指标数据
累计调用量760,159+ 次
支持版本V1 固定版 / V2 随机版
文案组合数V1: 7 种 / V2: 143 种
代码规模单文件,225 行
外部依赖

🔗 相关链接

链接地址
在线文档https://apis.uctb.cn/apidata?id=3
API 地址https://apis.uctb.cn/api/time
开源地址https://gitee.com/one-thousand-and-twenty-five/time

📝 更新日志

v1.0.0 (2025-05-01)

  • ✅ 初始版本发布
  • ✅ V1 固定文案版本(type=1)
  • ✅ V2 随机丰富版本(type=2),143 种文案组合
  • ✅ 7 个时间段全覆盖:凌晨/早晨/上午/中午/下午/傍晚/晚上
  • ✅ CORS 跨域支持,前端可直接调用
  • ✅ 参数校验与错误处理
  • ✅ 完全免费开放,无调用限制

📄 开源协议

本项目采用 MIT 协议 开源,可自由使用、修改和分发。

MIT License

Copyright (c) 2025 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

如果这个项目对你有帮助,给一个 ⭐ Star 支持一下吧! Made with ❤️ by YouChuang Open Source Community · Powered by PHP

1

—— 评论区 ——

博主关闭了所有页面的评论
人生倒计时
舔狗日记