note 89068 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 22 Mar 2009 18:43:45 +0000 |
| Subject: | note 89068 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152289@lists.php.net to get a copy of this message | ||
Note Submitter: PaulE
----
Had to find a function to get the first and last day of a week. Couldn't find any working
script, so I created this one. It's based on ISO 8601, that will mean that the week starts with
Monday, ends with Sunday and 4th January is always in the first week of the year.
<?php
/**
* Class to receive the start and ending date of a week
*/
class Week
{
const MONDAY = 1;
const TUESDAY = 2;
const WEDNESDAY = 3;
const THURSDAY = 4;
const FRIDAY = 5;
const SATURDAY = 6;
const SUNDAY = 7;
private $week_start;
private $week_end;
private $week;
private $year;
public function __construct()
{
$this -> week_start = self::MONDAY;
$this -> week_end = self::SUNDAY;
$this -> week = date("W");
$this -> year = date("Y");
}
/**
* This function sets the weeknumber of the year.
* Value of a week should be an INT which is greater
* then 0 and lower then 53.
*
* @param int $week Weeknumber of the year
*/
public function setWeek($week)
{
if(ctype_digit((string)$week) && $week > 0 && $week < 53)
{
$this -> week = $week;
}
}
/**
* This function sets the yearnumber. Value of a the
* year should be an INT which should have an length
* of 4 charaters.
*
* @param int $year Number of the year (2009 etc)
*/
public function setYear($year)
{
if(ctype_digit((string)$year) && strlen($year) == 4)
{
$this -> year = $year;
}
}
/**
* This function sets the starting or ending day of
* the week. It requires the type ('start' or 'end')
* and the day of the year which is available by
* using Week::MONDAY to Week::SUNDAY.
*
* @param string $type Type to set ('start' or 'end')
* @param int $day_number Day number (Week::MONDAY to Week::SUNDAY)
*/
public function setWeekday($type, $day_number)
{
if(ctype_digit($day_number) && $day_number > 0 && $day_number < 8)
{
switch($type)
{
case 'start':
$this -> week_start = $day_number;
break;
case 'end':
$this -> week_end = $day_number;
break;
}
}
}
/**
* This function calculates the starting and ending
* date for the given week and year.
*/
public function receiveDates()
{
$timestamp = strtotime('4 January ' . $this -> year . ' +' . ($this ->
week - 1) . ' weeks');
$difference_start = $this -> week_start - date("N", $timestamp);
$difference_end = $this -> week_end - date("N", $timestamp);
($difference_start > 0 ? $difference_start -= 7 : '');
return array(
'start' => strtotime("+" . $difference_start . " days",
$timestamp),
'end' => strtotime("+" . $difference_end . " days", $timestamp)
);
}
}
$WeekConverter = new Week();
for($i = 2008; $i <= 2010; $i++)
{
echo "<h2>Year: " . $i . "</h2>";
$WeekConverter -> setYear($i);
for($y = 1; $y < 53; $y++)
{
echo "<strong>Week: " . $y . "</strong><br />";
$WeekConverter -> setWeek($y);
foreach($WeekConverter -> receiveDates() as $type => $timestamp)
{
echo ucfirst($type) . "s: " . date("d-m-Y", $timestamp) . "<br
/>";
}
echo "<br />";
}
echo "<hr />";
}
?>