php教程

超轻量级php框架startmvc

PHP获取日期对应星期、一周日期、星期开始与结束日期的方法

更新时间:2020-03-28 12:32:43 作者:startmvc
本文实例讲述了PHP获取日期对应星期、一周日期、星期开始与结束日期的方法。分享给大家

本文实例讲述了PHP获取日期对应星期、一周日期、星期开始与结束日期的方法。分享给大家供大家参考,具体如下:


/*
 * 获取日期对应的星期
 * 参数$date为输入的日期数据,格式如:2018-6-22
 */
function get_week($date)
{
 //强制转换日期格式
 $date_str = date('Y-m-d', strtotime($date));
 //封装成数组
 $arr = explode("-", $date_str);
 //参数赋值
 //年
 $year = $arr[0];
 //月,输出2位整型,不够2位右对齐
 $month = sprintf('%02d', $arr[1]);
 //日,输出2位整型,不够2位右对齐
 $day = sprintf('%02d', $arr[2]);
 //时分秒默认赋值为0;
 $hour = $minute = $second = 0;
 //转换成时间戳
 $strap = mktime($hour, $minute, $second, $month, $day, $year);
 //获取数字型星期几
 $number_wk = date("w", $strap);
 //自定义星期数组
 $weekArr = array("0", "1", "2", "3", "4", "5", "6");
 //获取数字对应的星期
 return $weekArr[$number_wk];
}
/**
 * 获取一周日期
 * @param $time 时间戳
 * @param $format 转换格式
 */
function get_week($time, $format = "Y-m-d") {
 $week = date('w',$time);
 $weekname=array('星期一','星期二','星期三','星期四','星期五','星期六','星期日');
 //星期日排到末位
 if(empty($week)){
 $week=7;
 }
 for ($i=0;$i<=6;$i++){
   $data[$i]['date'] = date($format,strtotime( '+'. $i+1-$week .' days',$time));
   $data[$i]['week'] = $weekname[$i];
 }
 return $data;
}
/*
 * 获取某星期的开始时间和结束时间
 * time 时间
 * first 表示每周星期一为开始日期 0表示每周日为开始日期
 */
function getWeekMyActionAndEnd($time = '', $first = 1)
{
 //当前日期
 if (!$time) $time = time();
 $sdefaultDate = date("Y-m-d", $time);
 //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
 //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
 $w = date('w', strtotime($sdefaultDate));
 //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
 $week_start = date('Y-m-d', strtotime("$sdefaultDate -" . ($w ? $w - $first : 6) . ' days'));
 //本周结束日期
 $week_end = date('Y-m-d', strtotime("$week_start +6 days"));
 return array("week_start" => $week_start, "week_end" => $week_end);
}

PS:这里再为大家推荐几款时间及日期相关工具供大家参考:

在线日期/天数计算器: http://tools.jb51.net/jisuanqi/date_jisuanqi

在线日期计算器/相差天数计算器: http://tools.jb51.net/jisuanqi/datecalc

在线日期天数差计算器: http://tools.jb51.net/jisuanqi/onlinedatejsq

Unix时间戳(timestamp)转换工具: http://tools.jb51.net/code/unixtime

PHP 日期 星期 开始日期 结束日期
基础信息
请求
SQL查询
加载文件
配置
错误
总耗时: 89.43ms 内存: 90.17KB
请求方法: GET
请求URI: /article_7993.html
控制器: index
方法: index
PHP版本: 8.0.26
服务器: nginx/1.24.0
GET参数
无GET参数
POST参数
无POST参数
请求头
Accept-Encoding: gzip, br, zstd, deflate
User-Agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Accept: */*
Host: startmvc.com
Content-Length:
Content-Type:
Cookie
无Cookie数据
SELECT id,name FROM sm_article_category 0.00ms
SELECT a.id,a.category_id,a.title,a.keywords,a.description,a.update_time,ac.content FROM sm_article as a JOIN sm_article_content as ac ON ac.article_id = a.id WHERE a.id = '7993' LIMIT 1 0.00ms
SELECT tag_id FROM sm_tag_article WHERE article_id = '7993' 0.03ms
select id,title from sm_article where id in (select article_id from sm_tag_article where tag_id=5405) and status=1 order by id desc limit 20 0.00ms
总计:4 条SQL语句 总耗时:0.03 ms
1. /www/wwwroot/startmvc.com/public/index.php
2. /www/wwwroot/startmvc.com/startmvc/boot.php
3. /www/wwwroot/startmvc.com/startmvc/function.php
4. /www/wwwroot/startmvc.com/vendor/autoload.php
5. /www/wwwroot/startmvc.com/vendor/composer/autoload_real.php
6. /www/wwwroot/startmvc.com/vendor/composer/platform_check.php
7. /www/wwwroot/startmvc.com/vendor/composer/ClassLoader.php
8. /www/wwwroot/startmvc.com/vendor/composer/autoload_static.php
9. /www/wwwroot/startmvc.com/startmvc/core/Config.php
10. /www/wwwroot/startmvc.com/config/common.php
11. /www/wwwroot/startmvc.com/startmvc/core/Session.php
12. /www/wwwroot/startmvc.com/startmvc/core/App.php
13. /www/wwwroot/startmvc.com/startmvc/core/Exception.php
14. /www/wwwroot/startmvc.com/function/custom_function.php
15. /www/wwwroot/startmvc.com/function/global_function.php
16. /www/wwwroot/startmvc.com/startmvc/core/Request.php
17. /www/wwwroot/startmvc.com/startmvc/core/Middleware.php
18. /www/wwwroot/startmvc.com/startmvc/core/Router.php
19. /www/wwwroot/startmvc.com/config/route.php
20. /www/wwwroot/startmvc.com/app/home/controller/ArticleController.php
21. /www/wwwroot/startmvc.com/app/common/BaseController.php
22. /www/wwwroot/startmvc.com/startmvc/core/Controller.php
23. /www/wwwroot/startmvc.com/startmvc/core/Loader.php
24. /www/wwwroot/startmvc.com/startmvc/core/View.php
25. /www/wwwroot/startmvc.com/config/view.php
26. /www/wwwroot/startmvc.com/startmvc/core/Db.php
27. /www/wwwroot/startmvc.com/config/database.php
28. /www/wwwroot/startmvc.com/startmvc/core/db/DbCore.php
29. /www/wwwroot/startmvc.com/startmvc/core/db/DbInterface.php
30. /www/wwwroot/startmvc.com/runtime/temp/home/article/detail.php
31. /www/wwwroot/startmvc.com/startmvc/core/tpl/trace.php
总计:31 个文件
系统配置
运行环境: development
调试模式: 开启
时区设置: Asia/Shanghai
最大执行时间: 300秒
内存限制: 128M
上传限制: 50M
POST限制: 50M
字符集: UTF-8
错误报告级别: 32759
缓存状态
APC: 不可用
Memcached: 不可用
Redis: 不可用
OPcache: 可用
XCache: 不可用
File Cache: 可用
Autoload: 1 个加载器
Session: 已激活
框架配置
session: [Array]
trace: 1
debug: 1
default_controller: Index
default_action: index
异常信息
未捕获到异常
错误信息
未发现错误
应用运行正常,未发现错误或异常
89.43ms