3/25/2009

php get monday and sunday of this week and last week

i have used this function in my project.it seems works well :-)

code:

function get_monday_and_sunday_of_lastweek($str_date = "")
{
$mas = array();

if(empty($str_date)) $str_date = strtotime(date('Y-m-d'));
else $str_date = strtotime($str_date);

if(!$str_date)
{
return $mas;
}

if(date('w',$str_date) == 1)
{
$mas['last_monday'] = date('Y-m-d',strtotime('last Monday',$str_date));
}
else
{
$mas['last_monday'] = date('Y-m-d',strtotime('last Monday',strtotime('last Monday',$str_date)));
}

$mas['last_sunday'] = date('Y-m-d',strtotime('last Sunday',$str_date));

return $mas;
}

function get_monday_and_sunday_of_thisweek($str_date)
{
$mas = array();

if(empty($str_date)) $str_date = strtotime(date('Y-m-d'));
else $str_date = strtotime($str_date);

if(!$str_date)
{
return $mas;
}

if(date('w',$str_date) == 1)
{
$mas['monday'] = date('Y-m-d',$str_date);
}
else
{
$mas['monday'] = date('Y-m-d',strtotime('last Monday',$str_date));
}

if(date('w',$str_date) == 0)
{
$mas['sunday'] = date('Y-m-d',$str_date);
}
else
{
$mas['sunday'] = date('Y-m-d',strtotime('next Sunday',$str_date));
}
return $mas;
}
?>

No comments: