php取周的第一天和最后一天

http://php.net/manual/zh/function.strtotime.php


michal dot kocarek at brainbox dot cz

7 years ago

strtotime() also returns time by year and weeknumber. (I use PHP 5.2.8, PHP 4 does not support it.) Queries can be in two forms:

- "yyyyWww", where yyyy is 4-digit year, W is literal and ww is 2-digit weeknumber. Returns timestamp for first day of week (for me Monday)

- " yyyy-Www-d", where yyyy is 4-digit year, W is literal, ww is 2-digit weeknumber and dd is day of week (1 for Monday, 7 for Sunday)

<?php

// Get timestamp of 32nd week in 2009.

strtotime('2009W32'); // returns timestamp for Mon, 03 Aug 2009 00:00:00

// Weeknumbers < 10 must be padded with zero:

strtotime('2009W01'); // returns timestamp for Mon, 29 Dec 2008 00:00:00

// strtotime('2009W1'); // error! returns false

// See timestamp for Tuesday in 5th week of 2008

strtotime('2008-W05-2'); // returns timestamp for Tue, 29 Jan 2008 00:00:00

?>

Weeknumbers are (probably) computed according to ISO-8601 specification, so doing date('W') on given timestamps should return passed weeknumber.

//October 29, 2009 is my birthday
echo date('W', strtotime('2009-10-29'));
//OUTPUT: 44
//October 29 is the 44th week in the year 2009

//Here is the function to get the start and end dates given a week number:

function getWeekDates($year, $week, $start=true)
{
    $from = date("Y-m-d", strtotime("{$year}-W{$week}-1")); //Returns the date of monday in week
    $to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));   //Returns the date of sunday in week
 
    if($start) {
        return $from;
    } else {
        return $to;
    }
    //return "Week {$week} in {$year} is from {$from} to {$to}.";
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值