note 89068 added to function.date
| From: | PaulE at osu1 dot php dot net | Date: | Fri, 20 Feb 2009 06:47:32 +0000 |
| Subject: | note 89068 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150881@lists.php.net to get a copy of this message | ||
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 />";
}
?>
----
Server IP: 83.137.20.135
Probable Submitter: 145.53.158.69
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/89068
Del: integrated -- https://master.php.net/note/delete/89068/integrated
Del: useless -- https://master.php.net/note/delete/89068/useless
Del: bad code -- https://master.php.net/note/delete/89068/bad+code
Del: spam -- https://master.php.net/note/delete/89068/spam
Del: non-english -- https://master.php.net/note/delete/89068/non-english
Del: in docs -- https://master.php.net/note/delete/89068/in+docs
Del: other reasons-- https://master.php.net/note/delete/89068
Reject -- https://master.php.net/note/reject/89068
Search -- https://master.php.net/manage/user-notes.php