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

来源:互联网 发布:淘宝店铺第三方 编辑:程序博客网 时间:2024/06/11 17:11

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 birthdayecho 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
原创粉丝点击