• PHP通过当前日期判断是当月的第几周


    $now_week_num = self::current_week(strtotime($date));

    public static function current_week($date)
    {
    $week = self::get_week_block($date, $offset = 1);
    $wk_day = date('w', strtotime(date('Y-m-1 00:00:00', $date))) ?: 7; //今天周几
    $day = date('d', $date) - (8 - $wk_day); //今天几号
    $weekof = $day <= 0 ? 1 : ceil($day / 7) + 1; //计算是第几个星期
    if ($offset) {
    $week[0] = date('Y-m-d', max(strtotime(date('Y-m-1 00:00:00', $date)), $week[0]));
    $week[1] = date('Y-m-d', min(strtotime(date('Y-m-' . date('t', $date) . ' 23:59:59', $date)), $week[1]));
    } else {
    $week[0] = date('Y-m-d', $week[0]);
    $week[1] = date('Y-m-d', $week[1]);
    }
    return ['week' => $week, 'weekof' => $weekof];
    }

    public static function get_week_block($sdefaultDate = false, $first = 1)
    {
    //当前日期
    //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
    if (!$sdefaultDate) $sdefaultDate = time();
    if (is_numeric($sdefaultDate)) $sdefaultDate = date('Y-m-d', $sdefaultDate);
    //获取当前周的第几天 周日是 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(strtotime($week_start . ' 00:00:00'), strtotime($week_end . ' 23:59:59'));
    }
  • 相关阅读:
    ubuntu下ssh服务相关操作
    搜索引擎的基础-倒排索引
    mysql重置密码
    mysql 服务器配置
    Activiti如何替换已部署流程图
    循环select查询结果集
    存储过程范例,有输入输出参数,带回滚
    sql server中字符串无法替换空格的问题
    Myeclipse实用快捷键总结
    Linux中添加用户与删除用户
  • 原文地址:https://www.cnblogs.com/T8888/p/15668652.html
Copyright © 2020-2023  润新知