【问题标题】:How to Get The Given Months Number of Weeks and also week start from monday and end with friday. and also all week start date and end date如何获得给定的周数和周数,从星期一开始,到星期五结束。以及所有星期的开始日期和结束日期
【发布时间】:2013-11-23 03:08:21
【问题描述】:

我想得到所有星期的给定月份,我还想得到星期的开始日期和星期的结束日期,星期的持续时间是星期一到星期五。

【问题讨论】:

  • 你用谷歌搜索过“php 日期”吗? php.net/manual/en/function.date.php
  • 是的,我试过了,但我找不到从星期一开始到星期五结束的星期。
  • 请给我们看一些代码,你尝试了什么。如果你有 mondey 的 weekstart 日期,你只需简单地添加 4 天: date('Y-m-d',strtotime($weekstart." + 4 days");

标签: php date smarty


【解决方案1】:

以下是用于生成日历的非常简单的 PHP 脚本的一部分。您应该能够使用/修改它来回答您的所有问题:

class Calendar {
    public $calendar;
    public $current_date;
    public $current_month_days;
    public $current_date_str;
    public function __construct($month = FALSE, $expand = FALSE)
    {
        try {
            $date_obj = new DateTime($month);
            $this->current_date_str = $date_obj->getTimestamp();
        } catch ( Exception $e ) {
            $this->current_date_str = time();   
        }
        $this->current_date = date('Y-m', $this->current_date_str);
        $this->get_current_month_days();
        $this->build_calendar();
    }
    public function get_current_month_days()
    {
        $days_in_month = cal_days_in_month(CAL_GREGORIAN, date('m', strtotime($this->current_date)), date('Y', strtotime($this->current_date)));
        for ( $i = 1; $i <= $days_in_month; $i++ ) {
            $this->current_month_days[$this->current_date.'-'.sprintf('%02d', $i)] = '';
        }
        return $this->current_month_days;
    }
    public function build_calendar()
    {
        $this->calendar = array('row1' => array());
        $day_start = date('w', strtotime(key(array_slice($this->current_month_days, 0, 1))));
        if ( $day_start > 0 ) {
            $this->calendar['row1'] = array_pad($this->calendar['row1'], $day_start, FALSE);    
        }
        $i = $day_start;
        $row = 1;
        foreach ( $this->current_month_days as $k => $v ) {
            if ( $i == 7 ) {
                $i = 0;
                $row++; 
            }
            $this->calendar['row'.$row][$k] = $k;
            $i++;
        }
        if ( sizeof($this->calendar['row'.$row]) != 7 ) {
            $this->calendar['row'.$row] = array_pad($this->calendar['row'.$row], 7, FALSE);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 2012-11-27
    • 2020-03-24
    • 2010-11-19
    相关资源
    最近更新 更多