{"id":20577726,"url":"https://github.com/zjkal/time-helper","last_synced_at":"2025-04-05T12:07:07.485Z","repository":{"id":57852262,"uuid":"528501322","full_name":"zjkal/time-helper","owner":"zjkal","description":"一个简单快捷的PHP日期时间助手类库。 a smart PHP datetime helper library.","archived":false,"fork":false,"pushed_at":"2024-03-14T17:19:17.000Z","size":5338,"stargazers_count":164,"open_issues_count":0,"forks_count":16,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-03-14T18:56:33.602Z","etag":null,"topics":["composer","date","helpers","libraries","php","time","tool"],"latest_commit_sha":null,"homepage":"https://time.0x1.site","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zjkal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-08-24T16:16:32.000Z","updated_at":"2024-04-24T06:59:59.367Z","dependencies_parsed_at":"2023-09-25T05:12:28.458Z","dependency_job_id":"083e6805-08ae-40e1-a9a9-56db9d31288d","html_url":"https://github.com/zjkal/time-helper","commit_stats":{"total_commits":71,"total_committers":2,"mean_commits":35.5,"dds":"0.014084507042253502","last_synced_commit":"308be4f24137b71e80578d4bcd7d7143d54ec775"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zjkal%2Ftime-helper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zjkal%2Ftime-helper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zjkal%2Ftime-helper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zjkal%2Ftime-helper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zjkal","download_url":"https://codeload.github.com/zjkal/time-helper/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247332609,"owners_count":20921853,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composer","date","helpers","libraries","php","time","tool"],"created_at":"2024-11-16T06:08:19.522Z","updated_at":"2025-04-05T12:07:07.465Z","avatar_url":"https://github.com/zjkal.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cbr/\u003e\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://cdn.0x1.site/logo-time-helper.svg\" alt=\"TimeHelper\" width=\"180\" /\u003e\n    \u003cbr/\u003e\n    \u003cbr/\u003e\n    中文 | \u003ca href=\"https://github.com/zjkal/time-helper/blob/main/README_EN.md\" target=\"_blank\"\u003eEnglish\u003c/a\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/zjkal/time-helper/blob/main/LICENSE\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://poser.pugx.org/zjkal/time-helper/license\" alt=\"License\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/zjkal/time-helper\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://poser.pugx.org/zjkal/time-helper/require/php\" alt=\"PHP Version Require\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/zjkal/time-helper\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://poser.pugx.org/zjkal/time-helper/v\" alt=\"Latest Stable Version\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://packagist.org/packages/zjkal/time-helper\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://poser.pugx.org/zjkal/time-helper/downloads\" alt=\"Total Downloads\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/zjkal/time-helper\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/github/actions/workflow/status/zjkal/time-helper/.github/workflows/composer.yml?branch=main\" alt=\"GitHub Workflow Status\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n`TimeHelper` 是一个简单易用的`PHP时间日期助手类库`,可以快速实现常用的时间日期操作,比如获取指定时间的秒数,获取友好的时间格式,判断时间范围,计算两个时间相差值,返回N小时/天/星期/月/年前或者后的时间戳等等\n\n## 🧩特性\n\n- 简单易用: 不依赖任何扩展,`开箱即用`\n- 化繁为简: 所有方法都可以传入`任意类型的时间日期格式`或`时间戳`\n- 快捷高效: 所有操作只需要`一个静态方法`即可完成\n- 长期维护: 作者为自由职业者,保证项目的`长期稳定`和`持续更新`\n\n## 🚀安装\n\n通过Composer导入类库\n\n```bash\ncomposer require zjkal/time-helper\n```\n\n## 🌈使用文档\n\n查看[使用文档](https://time.0x1.site/)或阅读以下简要说明\n\n### 1. 获取需要的秒数\n\n*一般用于设置缓存时间,设置结束时间等*\n\n```php\n//返回到今天晚上零点之前的秒数\nTimeHelper::secondEndToday();\n\n//返回N分钟的秒数(默认为1分钟)\nTimeHelper::secondMinute(5);\n\n//返回N小时的秒数(默认为1小时)\nTimeHelper::secondHour(2);\n\n//返回N天的秒数(默认为1天)\nTimeHelper::secondDay(10);\n\n//返回N周的秒数(默认为1周)\nTimeHelper::secondWeek(4);\n```\n\n### 2. 返回友好的日期格式,比如N秒前,N分钟前,N小时前等等\n\n*一般用于社交类平台,评论,论坛等*\n\n```php\n//一共2个参数:\n//第1个参数传入字符串类型的时间或者时间戳都可以，\n//第2个参数为语言(默认为中文,需要英文请传入en)\nTimeHelper::toFriendly('2022-3-2 10:15:33');\n//英文\nTimeHelper::toFriendly(1646186290, 'en');\n```\n\n### 3. 判断时间范围\n\n```php\n//判断日期是否为今天\nTimeHelper::isToday('2020-4-10 23:01:11');\n\n//判断日期是否为昨天\nTimeHelper::isYesterday('2020-4-10 23:01:11');\n\n//判断日期是否为明天\nTimeHelper::isTomorrow('2020-4-10 23:01:11');\n\n//判断日期是否为本周\nTimeHelper::isThisWeek('2020-5-1');\n\n//判断日期是否为本月\nTimeHelper::isThisMonth(1586451741);\n\n//判断日期是否为今年\nTimeHelper::isThisYear('Apr 11, 2020');\n\n//判断指定时间是星期几,不传默认为当前时间. 返回值为1-7,1为星期一,7为星期日\nTimeHelper::getWeekDay('2022-11-27');\n\n//判断指定时间是否为平常日(周一到周五)\nTimeHelper::isWeekday('2023-03-08');\n\n//判断指定时间是否为周末(周六和周日)\nTimeHelper::isWeekend(1586451741);\n\n\n```\n\n### 4. 计算两个时间相差值\n\n*如果只传入一个参数,则与当前时间比较*\n\n```php\n//计算两个日期相差的秒数\nTimeHelper::diffSeconds('2022-4-10 23:01:11',1586451741)\n\n//计算两个日期相差的分钟数\nTimeHelper::diffMinutes('Apr 11, 2020 11:51:21','Apr 11, 2020 11:31:21')\n\n//计算两个日期相差的小时数\nTimeHelper::diffHours(1586451741,1586491741)\n\n//计算两个日期相差天数\nTimeHelper::diffDays('2022-4-10 23:01:11','Apr 11, 2020');\n\n//计算两个日期相差周数\nTimeHelper::diffWeeks('2022-4-10 23:01:11');\n\n//计算两个日期相差月数\nTimeHelper::diffMonths(1586451741,'Apr 11, 2020');\n\n//计算两个日期相差年数\nTimeHelper::diffYears('2022-4-10 23:01:11','Apr 11, 2020');\n\n//比较两个时间的大小,如果第二个参数为空,则与当前时间比较\n//第一个时间大于第二个时间则返回1,小于则返回-1,相等时则返回0\nTimeHelper::compare('2022-4-10 23:01:11','Apr 11, 2020');\n```\n\n### 5. 返回N小时/天/星期/月/年前或者后的时间戳\n\n*只传入1个参数以当前时间计算,传入第2个参数则以该时间计算,传入第3个参数为true,则时间取整*\n\n```php\n//返回指定时间3分钟前0秒的时间戳\nTimeHelper::beforeMinute(3,'2022-3-2 10:15:33',true);\n\n//返回当前时间5分钟后的时间戳\nTimeHelper::afterMinute(5);\n\n//返回指定时间1小时前的时间戳(请注意此用法为php8之后的用法)\nTimeHelper::beforeHour(datetime:'Apr 11, 2020');\n\n//返回2小时后的时间戳\nTimeHelper::afterHour(2);\n\n//返回15天前0点的时间戳\nTimeHelper::beforeDay(15,null,true);\n\n//返回15天后的时间戳\nTimeHelper::afterDay(15);\n\n//返回指定时间2星期前的时间戳\nTimeHelper::beforeWeek(2,'2022-4-10 23:01:11');\n\n//返回指定时间10星期后的时间戳\nTimeHelper::afterWeek(10,1646360133);\n\n//返回指定时间1个月前的时间戳(请注意此用法为php8之后的用法)\nTimeHelper::beforeMonth(datetime:1646360133);\n\n//返回5个月后的时间戳\nTimeHelper::afterMonth(5);\n\n//返回指定时间3年前的时间戳\nTimeHelper::beforeYear(3,'2022-7-11');\n\n//返回2年后的时间戳\nTimeHelper::afterYear(2);\n```\n\n### 6.获取当前秒级/毫秒级/微秒级/纳秒级的时间戳\n\n*生成订单号或者与其他编程语言对接时可能会用到*\n\n```php\n//获取秒级的时间戳,可用time()代替\nTimeHelper::getTimestamp();\n\n//获取毫秒级的时间戳\nTimeHelper::getMilliTimestamp();\n\n//获取微秒级的时间戳\nTimeHelper::getMicroTimestamp();\n\n//获取纳秒级的时间戳\nTimeHelper::getNanoTimestamp();\n```\n\n### 7.日期转换\n\n*用于爬虫爬取网页或第三方程序对接时,时间格式不统一的转换*\n\n```php\n//将任意格式的时间转换为指定格式\n//第一个参数是时间格式,与系统函数date()的格式保持一致\n//第二个参数则是任意格式的时间日期,不传则默认为当前时间,可用系统函数date()代替\nTimeHelper::format('Y-m-d H:i:s','May 3, 2022');\n\n//判断一个字符串是否为时间戳,是返回true,否返回false\nTimeHelper::isTimestamp(1646360133);\n\n//将任意时间类型的字符串转为时间戳\nTimeHelper::toTimestamp('Apr 11, 2020');\n\n//根据日期字符串, 修改指定时间, 并返回修改后的时间戳\nTimeHelper::modifyTimestamp('+1 day', '2023-9-1 12:00:00');\n\n//获取当前时间(默认格式为Y-m-d H:i:s)\nTimeHelper::now();\n```\n\n### 8.平闰年相关\n\n*比原生方法使用起来更方便*\n\n```php\n//判断是否为闰年,是返回true,否返回false\nTimeHelper::isLeapYear('2020-3-2 10:15:33');\n\n//判断该日期的当年有多少天\nTimeHelper::daysInYear(1646360133);\n\n//判断该日期的当月有多少天\nTimeHelper::daysInMonth('Apr 11, 2020');\n```\n\n### 9.时区相关 🆕\n\n*主要提供获取不同时区的时间和转换不同时区的时间的方法*\n\n```php\n//将任意格式的时间或时间戳转换为指定时区的时间\n//第一个参数为要转换的目标时区\n//第二个参数为原时区,不传则默认为当前时区\n//第三个参数为任意格式的时间或时间戳,不传则默认为当前时间\n//第四个参数为时间格式,与系统函数date()的格式保持一致,不传则默认为Y-m-d H:i:s\nTimeHelper::timezoneFormat('Europe/London', 'Asia/Shanghai', '2023-8-15 19:16:43', 'H:i:s');\n//获得当前洛杉矶的时间\nTimeHelper::timezoneFormat('America/Los_Angeles');\n//将洛杉矶时间转换为伦敦时间\nTimeHelper::timezoneFormat('Europe/London', 'America/Los_Angeles', 'Aug 15, 2023 10:15:33', 'H:i:s');\n//将时间戳转换为伦敦时间\nTimeHelper::timezoneFormat('Europe/London', null, 1692097543);\n```\n\n所支持的时区列表请参考[时区列表](https://www.php.net/manual/zh/timezones.php)\n\n### 10.国内节假日/工作日相关 🔥\n\n*专门针对国内的节假日进行判断,目前包含2020年-2024年的节假日数据,后续也会持续更新.*\n*为了便于维护,另起了一个类ChinaHoliday,同样可以传入任意类型的时间格式或时间戳*\n\n```php\nuse zjkal\\ChinaHoliday;\n\n//判断指定日期是否为国内的工作日\nChinaHoliday::isWorkday('2023-01-23');\n\n//判断指定日期是否为国内的节假日\nChinaHoliday::isHoliday(1646360133);\n```\n\n\u003e **特别说明**: 所有时间的方法都可以传入任意格式的时间或者时间戳, 但是有一点请注意 m/d/y 或 d-m-y 格式的日期，如果分隔符是斜线（/），则使用美洲的 m/d/y 格式。如果分隔符是横杠（-）或者点（.），则使用欧洲的 d-m-y 格式。为了避免潜在的错误，您应该尽可能使用 YYYY-MM-DD 格式或其他格式.\n\n## 📃更新日志\n\n\u003e v1.1.12 2025年1月23日\n\u003e * 增加了对PHP8.4的兼容性\n\n\u003e v1.1.11 2024年12月17日\n\u003e * 修复了由于PHP运行环境的时区设置差异, 可能引起的节假日数据计算错误的BUG\n\n\u003e v1.1.10 2024年11月16日\n\u003e * 增加了2025年的中国节假日数据\n\n\u003e v1.1.9 2024年7月2日\n\u003e * 增加了获取当前时间的方法now()\n\n\u003e v1.1.8 2024年6月6日\n\u003e * 修复了获取友好时间的错误\n\n\u003e v1.1.7 2024年6月1日\n\u003e * 增加了计算两个日期相差秒数的方法diffSeconds()\n\u003e * 增加了计算两个日期相差分钟数的方法diffMinutes()\n\u003e * 增加了计算两个日期相差小时数的方法diffHours()\n\u003e * 增加了根据日期字符串, 修改指定时间的方法modifyTimestamp()\n\u003e * 精简代码\n\n\u003e v1.1.6 2024年4月25日\n\u003e * 增加了判断是否为昨天的方法isYesterday()\n\u003e * 增加了判断是否为明天的方法isTomorrow()\n\n\u003cdetails\u003e\u003csummary\u003e点击查看更多\u003c/summary\u003e\n\n\u003e v1.1.5 2024年2月5日\n\u003e * 精简和优化代码\n\n\u003e v1.1.4 2023年10月25日\n\u003e * 增加了2024年国内节假日的数据\n\n\u003e v1.1.3 2023年10月19日\n\u003e * 提升了toTimestamp()方法的兼容性,可以转换一些特殊格式的时间了\n\n\u003e v1.1.2 2023年9月16日\n\u003e * 增加了比较两个时间早晚的方法compare()\n\n\u003e v1.1.1 2023年8月15日\n\u003e * 增加了时区相关的方法,可以将任意格式的时间或时间戳转换为指定时区的时间\n\n\u003e v1.1.0 2023年3月19日\n\u003e * 增加了ChinaHoliday类,专门用于维护国内节假日相关的方法\n\u003e * 增加了判断是否为国内工作日的方法isWorkday()\n\u003e * 增加了判断是否为国内节假日的方法isHoliday()\n\n\u003e v1.0.10 2023年3月13日\n\u003e * 增加判断指定日期是否为平常日的方法isWeekday()\n\u003e * 增加判断指定日期是否为周末的方法isWeekend()\n\n\u003e v1.0.9 2023年3月8日\n\u003e * 修改toTimestamp()方法不传参数或传入空参数时,不再抛出异常,而是返回当前时间戳\n\u003e * 增加了判断是否是闰年的方法isLeapYear()\n\u003e * 增加了获取该日期当年的天数的方法daysInYear()\n\u003e * 增加了获取该日期当月的天数的方法daysInMonth()\n\n\u003e v1.0.8 2023年2月21日\n\u003e * 增加了判断指定时间是星期几的方法getWeekDay()\n\n\u003e v1.0.7 2023年2月8日\n\u003e * 增加了将任意格式的时间转换为指定格式的方法format()\n\u003e * 删除废弃方法friendly_date(),请使用toFriendly()代替\n\n\u003e v1.0.6 2023年1月27日\n\u003e * 修复BUG\n\n\u003e v1.0.5 2023年1月16日\n\u003e * 优化了友好的时间显示算法\n\n\u003e v1.0.4 2023年1月8日\n\u003e * 修复了变量类型的BUG\n\n\u003e v1.0.3 2023年1月7日\n\u003e * 增加了判断一个字符串是否为时间戳的方法\n\u003e * 增加了将任意时间类型的字符串转为时间戳的方法\n\u003e * 增加了获取秒级/毫秒级/微秒级/纳秒级的时间戳的方法\n\n\u003e v1.0.2 2023年1月5日\n\u003e * 增加了返回N分钟/小时的秒数的方法\n\n\u003e v1.0.1 2023年1月4日\n\u003e * 返回N分钟/小时/天/月/年前或后的时间戳所有方法,增加了是否取整的参数.\n\n\u003e v1.0.0 2022年8月25日\n\u003e * 从tp-helper的时间类中独立出来\n\u003c/details\u003e\n\n## 😎开发者们\n\n\u003c!-- readme: contributors -start --\u003e\n\u003ctable\u003e\n\t\u003ctbody\u003e\n\t\t\u003ctr\u003e\n            \u003ctd align=\"center\"\u003e\n                \u003ca href=\"https://github.com/zjkal\"\u003e\n                    \u003cimg src=\"https://avatars.githubusercontent.com/u/15082976?v=4\" width=\"100;\" alt=\"zjkal\"/\u003e\n                    \u003cbr /\u003e\n                    \u003csub\u003e\u003cb\u003ezjkal\u003c/b\u003e\u003c/sub\u003e\n                \u003c/a\u003e\n            \u003c/td\u003e\n            \u003ctd align=\"center\"\u003e\n                \u003ca href=\"https://github.com/miss-sugar\"\u003e\n                    \u003cimg src=\"https://avatars.githubusercontent.com/u/121731951?v=4\" width=\"100;\" alt=\"miss-sugar\"/\u003e\n                    \u003cbr /\u003e\n                    \u003csub\u003e\u003cb\u003emiss-sugar\u003c/b\u003e\u003c/sub\u003e\n                \u003c/a\u003e\n            \u003c/td\u003e\n            \u003ctd align=\"center\"\u003e\n                \u003ca href=\"https://github.com/iuyes\"\u003e\n                    \u003cimg src=\"https://avatars.githubusercontent.com/u/2977219?v=4\" width=\"100;\" alt=\"iuyes\"/\u003e\n                    \u003cbr /\u003e\n                    \u003csub\u003e\u003cb\u003e李云龙\u003c/b\u003e\u003c/sub\u003e\n                \u003c/a\u003e\n            \u003c/td\u003e\n            \u003ctd align=\"center\"\u003e\n                \u003ca href=\"https://github.com/ageChen\"\u003e\n                    \u003cimg src=\"https://avatars.githubusercontent.com/u/71121939?v=4\" width=\"100;\" alt=\"ageChen\"/\u003e\n                    \u003cbr /\u003e\n                    \u003csub\u003e\u003cb\u003eageChen\u003c/b\u003e\u003c/sub\u003e\n                \u003c/a\u003e\n            \u003c/td\u003e\n            \u003ctd align=\"center\"\u003e\n                \u003ca href=\"https://github.com/dkoin\"\u003e\n                    \u003cimg src=\"https://avatars.githubusercontent.com/u/5207234?v=4\" width=\"100;\" alt=\"dkoin\"/\u003e\n                    \u003cbr /\u003e\n                    \u003csub\u003e\u003cb\u003edkoin\u003c/b\u003e\u003c/sub\u003e\n                \u003c/a\u003e\n            \u003c/td\u003e\n            \u003ctd align=\"center\"\u003e\n                \u003ca href=\"https://github.com/wolf-leo\"\u003e\n                    \u003cimg src=\"https://avatars.githubusercontent.com/u/37436228?v=4\" width=\"100;\" alt=\"wolf-leo\"/\u003e\n                    \u003cbr /\u003e\n                    \u003csub\u003e\u003cb\u003ewolfcode\u003c/b\u003e\u003c/sub\u003e\n                \u003c/a\u003e\n            \u003c/td\u003e\n\t\t\u003c/tr\u003e\n\t\u003ctbody\u003e\n\u003c/table\u003e\n\u003c!-- readme: contributors -end --\u003e\n\n## ❤️由 JetBrains 赞助\n\n非常感谢 Jetbrains 为我提供的 IDE 开源许可，让我完成此项目和其他开源项目上的开发工作。\n\n[![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://www.jetbrains.com/)\n\n## 🐧扫以下二维码加入QQ频道\n\n\u003ca href=\"https://pd.qq.com/s/7h2hvcuxs\"\u003e\n  \u003cimg src=\"https://cdn.0x1.site/qrcode-qqpd.png\"  alt=\"QQ频道-世界上最好的编程语言\"/\u003e\n\u003c/a\u003e\n\n## 📖开源协议\n\nTimeHelper遵循[MIT开源协议](https://github.com/zjkal/time-helper/blob/main/LICENSE), 意味着您无需任何授权, 即可免费将TimeHelper应用到您的项目中\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzjkal%2Ftime-helper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzjkal%2Ftime-helper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzjkal%2Ftime-helper/lists"}