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

Posted on

In PHP, how to know how many mondays have passed in this month uptil today? – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and date . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about 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

That sounds like a pretty straightforward division calculation. From the current date, subtract number of days past last monday (example: wednesday = -2), divide it by 7 and ceil() it to round it up.

EDIT: That will include the current monday in the number, returning “3” for monday 21st.


 function mondays_get($month, $stop_if_today = true) {

$timestamp_now = time();

for($a = 1; $a < 32; $a++) {

    $day = strlen($a) == 1 ? "0".$a : $a;
    $timestamp = strtotime($month . "-$day");
    $day_code = date("w", $timestamp);
    if($timestamp > $timestamp_now)
    if($day_code == 1)


return $mondays;

echo mondays_get('2011-02');

Hope this is of use to you! i’ve just rolled it up.

“Beware of bugs in the above code; I have only proved it correct, not tried it.”

Works OK afaik

You could loop through all the days until now and count the mondays:

$firstDate = mktime(0, 0, 0, date("n"), 1, date("Y"));
$now = time();
$mondays = 0;
for ($i = $firstDate; $i < $now; $i = $i + 24*3600) {
    if (date("D", $i) == "Mon")
        $mondays ++;

Haven’t tested this script

Try this…

//find the most recent monday (doesn't find today if today is Monday though)
$startDate = strtotime( 'last monday' );

//if 'last monday' was not this month, 0 mondays.  
//if 'last monday' was this month, count the weeks
$mondays = date( 'm', $startDate ) != date( 'm' ) 
         ? 0 
         : floor( date( 'd', $startDate ) / 7 );

//increment the count if today is a monday (since strtotime didn't find it)
if ( date( 'w' ) == 1 ) $mondays++;

Another way is to find what day of the week is today, find the first such day of the month via some magic strtotime(), then calculate the difference between that and now in weeks. See below for a function that will take a Y-m-d formatted date() and return which weekday of the month it is.

Note: strtotime needs to be verbose, including “of” and the month: “first Monday of 2011-02” otherwise it advances one day. This bit me when I was testing edge cases.

Also added some display pepper which is completely optional but I felt like it.

function nthWeekdayOfMonth($day) {

    $dayTS = strtotime($day) ;

    $dayOfWeekToday = date('l', $dayTS) ;

    $firstOfMonth = date('Y-m', $dayTS) . "-01" ;
    $firstOfMonthTS = strtotime($firstOfMonth) ;

    $firstWhat = date('Y-m-d', strtotime("first $dayOfWeekToday of $monthYear", $firstOfMonthTS)) ;
    $firstWhatTS = strtotime($firstWhat) ;

    $diffTS = $dayTS - $firstWhatTS ;
    $diffWeeks = $diffTS / (86400 * 7);

    $nthWeekdayOfMonth = $diffWeeks + 1;

    return $nthWeekdayOfMonth ;

$day = date('Y-m-d') ;
$nthWeekdayOfMonth = nthWeekdayOfMonth($day) ;

switch ($nthWeekdayOfMonth) {
    case 1: 
        $inflector = "st" ;
        break ;
    case 2: 
        $inflector = "nd" ;
        break ;
    case 3: 
        $inflector = "rd" ;
        break ;
        $inflector = "th" ;

$dayTS = strtotime($day) ;

$monthName = date('F', $dayTS) ;    
$dayOfWeekToday = date('l', $dayTS) ;

echo "Today is the {$nthWeekdayOfMonth}$inflector $dayOfWeekToday in $monthName" ;

Leave a Reply

Your email address will not be published. Required fields are marked *