Search code examples
phpdaterecurrence

In PHP, how to know how many mondays have passed in this month uptil today?


Assume today is Feb 21, 2011 ( Monday ). It is the third Monday of this month. If date is given as input, How can I know how many Mondays have passed before it?

In PHP, how to know how many mondays have passed in this month uptil today?


Solution

  • $now=time() + 86400;
    if (($dow = date('w', $now)) == 0) $dow = 7; 
    $begin = $now - (86400 * ($dow-1));
    
    echo "Mondays: ".ceil(date('d', $begin) / 7)."<br/>";
    

    works for me....

    EDIT: includes today's monday too