ScoRpion
ScoRpion

Reputation: 11474

Count The Number of Sundays Till Today for the current Year and current month?

How can I calculate the number of Sundays that have already passed from the current YEAR till today. I would also like to calculate the number of Sundays that have already passed from the current MONTH till today.

eg

today is 14 April 2012 I should get 2 for the Number of Sundays
that are passed from the current month.

Can anyone give me a hint or a tutorial how this can be achieved ?

Upvotes: 1

Views: 1653

Answers (2)

Ashim Saha
Ashim Saha

Reputation: 162

Date('W') might help you. Check php manual for the format - http://php.net/manual/en/function.date.php .

Do not forget according to manual - ISO-8601 week number of year, weeks starting on Monday. So this week will be counted even if this not Sunday yet. So in that case reduce the number by 1.

function get_sunday_since_year_start($today=null)
{
  if($today==null) $today = time();

  if(date('D',$today)=='Sun')
    return Date('W',$today);
  else
    return Date('W',$today)-1;
}

function get_sunday_since_month_start()
{
   return get_sunday_since_year_start()-get_sunday_since_year_start(strtotime(date('Y-m-01 00:00:00')));
}

Upvotes: 0

Vad.Gut
Vad.Gut

Reputation: 531

well i guess it is simple enough using the date() function

//will give you the amount of sundays from the begining of the year
$daysTotal = ceil((date("z") - date("w")) / 7); 
//will give you the amount of sundays from the begining of the month
$daysTotal = ceil((date("j") - date("w")) / 7); 

I didn't test it, you might want to check if the round() function works right in this situation but the point is passed i think

good luck

Upvotes: 5

Related Questions