note 78858 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 09 Dec 2007 13:37:41 +0000 |
| Subject: | note 78858 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-132999@lists.php.net to get a copy of this message | ||
Note Submitter: kirti dot contact at gmail dot com
----
I was looking for a function to find the week range for a particular week. But now I have developed
by own. This function takes three arguments all optional
1. Week - This value ranges from 1- 52 or 53. It needs a correct value. If no value is provided, it
then uses the current week.
2. Year - If none is provided, then the current year.
3. Format - defaults to d/M/Y
It returns an array containing the 'start date' and the 'end date'
Imp. Week starts from Sunday.
function getWeekDateRange($week="",$year="",$format="d/M/Y"){
if($year=="" || !is_numeric($year))
$year=date("Y");
if($week=="" || !is_numeric($week))
$week=date("W");
$week--;
$first_day=date("w",mktime(0,0,0,1,1,$year));
$first_day_tstamp=mktime(0,0,0,1,1,$year);
$first_sunday=($first_day==0?0:(6-$first_day+1));
$first_sunday_tstamp=$first_sunday*24*3600+$first_day_tstamp;
if($first_sunday!=0){
$week_cal=$week-1;
}else{
$week_cal=$week;
}
$tw_start_tstamp=$first_sunday_tstamp+$week_cal*7*24*3600;
$tw_end_tstamp=$first_sunday_tstamp+($week_cal+1)*7*24*3600-1;
$yy=date("Y",$tw_end_tstamp);
if($yy>$year)
$tw_end_tstamp=mktime(23,59,59,12,31,$year);
if($week==0 && $first_sunday!=0){
$tw_start_tstamp=$first_day_tstamp;
$tw_end_tstamp=$first_sunday_tstamp-1;
}
$start_date=date($format,$tw_start_tstamp);
$end_date=date($format,$tw_end_tstamp);
return array($start_date,$end_date);
}