php获取当前周的起止日期,PHP获取一年有多少周和每周开始和结束日期

class Week extends Common{

public function get_week_all($year)

{

$whole_week = $this->get_big_week($year);//获取最大周数

$str = '';

for ($i = 1; $i <= $whole_week; $i++) {

$str .= $i . ',';

}

$str_arr = explode(',', rtrim($str, ','));//转成数组

$week_list = array();

foreach ($str_arr as $key => $value) {

$week_list[$key]['num'] = $value;

$week = $this->get_year_month_week($year, $value);//一周的日期

$week_list[$key]['detail'] = "第" . $value . "周(" . $week[0] . "至" . $week[6] . ")";

}

return $week_list;

}

/*

用法:

$this->get_date("Y-m-d","+1 day");当前日期加一天

$this->get_date("Y-m-d","+1 month",$date);指定日期下一月

$this->get_date("Y-m","+1 years",$date);指定日期下一年的年月

$this->get_date("Y-m-t",NULL,$date);指定日期的月底

$this->get_date("m","+1 month",$date);指定日期下一个月的月份(获取月份时候,日期必填。不然个别日期会有bug存在)

等等,根据需求自己获取。代码里面strtotime获取日期的地方都会变成公共方法调用。

*/

public function get_date($return, $string, $date)

{

if (empty($return)) {

$return = "Y-m-d";

}

if (empty($string)) {

$string = "now";

}

if (empty($date)) {

$date = time();

} else {

$date = strtotime(date($date));

}

if (preg_match("/month/", $string)) {

$first_day_of_month = date('Y-m', $date) . '-01';

$str_date = strtotime($first_day_of_month);

}

if (preg_match("/month/", $string)) {

$l_date = date("Y-m-d", strtotime($string, $str_date));

$day = substr(date("Y-m-d", $date), 8, 2);

if (substr($l_date, 0, 7) . '-' . date('t', strtotime(substr($l_date, 0, 7))) < substr($l_date, 0, 7) . '-' . $day) {

$l_date = substr($l_date, 0, 7) . '-' . date('t', strtotime(substr($l_date, 0, 7)));

} else {

$l_date = substr($l_date, 0, 7) . '-' . $day;

}

$l_date = date($return, strtotime($l_date));

return $l_date;

} else {

return date($return, strtotime($string, $date));

}

}

public function get_big_week($year_now)

{

$big_week = $this->get_date("W", NULL, $year_now . '-12-31');

if ($big_week == 1) {

$big_week = $this->get_date("W", NULL, $year_now . '-12-30');

}

if ($big_week == 1) {

$big_week = $this->get_date("W", NULL, $year_now . '-12-29');

}

if ($big_week == 1) {

$big_week = $this->get_date("W", NULL, $year_now . '-12-28');

}

if ($big_week == 1) {

$big_week = $this->get_date("W", NULL, $year_now . '-12-27');

}

if ($big_week == 1) {

$big_week = $this->get_date("W", NULL, $year_now . '-12-26');

}

if ($big_week == 1) {

$big_week = $this->get_date("W", NULL, $year_now . '-12-25');

}

if ($big_week == 1) {

$big_week = $this->get_date("W", NULL, $year_now . '-12-24');

}

return $big_week;

}

public function get_year_month_week($year_now, $week_now)

{

//指定年和周的日期数组

$time = new \DateTime();

$time->setISODate($year_now, $week_now, 1);

$result[] = $time->format('Y-m-d');

$time->setISODate($year_now, $week_now, 2);

$result[] = $time->format('Y-m-d');

$time->setISODate($year_now, $week_now, 3);

$result[] = $time->format('Y-m-d');

$time->setISODate($year_now, $week_now, 4);

$result[] = $time->format('Y-m-d');

$time->setISODate($year_now, $week_now, 5);

$result[] = $time->format('Y-m-d');

$time->setISODate($year_now, $week_now, 6);

$result[] = $time->format('Y-m-d');

$time->setISODate($year_now, $week_now, 7);

$result[] = $time->format('Y-m-d');

return $result;

}

}

调用方法:

$Week = new Week();

print_r($Week->get_week_all('2019'));

6cb54ca8a8608e00e1797ba4ce754b9a.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值