WordPress calendar_week_mod() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Helper function

Definition

In WordPress, calendar_week_mod() is a small helper used by get_calendar(). It takes a number and wraps it into the range 0 to 6, like a "mod 7" for the days of the week.

It also works for negative numbers, which is why it is used to find how many empty cells come before the first day of the month.

Syntax

Pass one whole number. You get a number from 0 to 6 back.

WordPress
<?php
$day = calendar_week_mod( 8 );
echo $day;
CallWhat it does
calendar_week_mod( 8 )Returns 1 (8 is one day after a full week).
calendar_week_mod( 14 )Returns 0 (two full weeks).
calendar_week_mod( -1 )Returns 6 (one step back from 0).

For normal use, call get_calendar(). This helper is for custom calendar code.

Full declaration
WordPress
calendar_week_mod( int $num ): int

Parameters

ParameterDescription
$numint. The number to wrap into the 0 to 6 range.

Return Value

Return ValueDescription
intThe remainder after dividing by 7, always from 0 to 6.

WordPress Examples

Three short theme examples. Each one shows the code and what it prints.

Example 1 — Wrap a number into a week

See the result for three inputs.

WordPress
<?php
echo calendar_week_mod( 3 ) . ', ';
echo calendar_week_mod( 8 ) . ', ';
echo calendar_week_mod( 14 );

How It Works

  1. 3 is already between 0 and 6, so it stays 3.
  2. 8 minus one full week (7) is 1.
  3. 14 is exactly two weeks, so the result is 0.

Example 2 — Negative numbers

Step back from the start of the week.

WordPress
<?php
echo calendar_week_mod( -1 ) . ', ';
echo calendar_week_mod( -7 ) . ', ';
echo calendar_week_mod( -8 );

How It Works

  1. -1 is one day before day 0, which is day 6.
  2. -7 is a full week back, so it is 0.
  3. -8 is one more day back, so it is 6 again.

Example 3 — Empty cells before day 1

Find how many blank cells start a calendar row.

WordPress
<?php
$first_day = 3;      // month starts on Wednesday (Sunday = 0)
$start_of_week = 1;  // week starts on Monday
$blank = calendar_week_mod( $first_day - $start_of_week );
echo 'Blank cells: ' . $blank;

How It Works

  1. The month starts on day 3 and the week starts on day 1.
  2. The difference is 2, so we need 2 empty cells first.
  3. calendar_week_mod() keeps the answer safe even when the difference is negative.

Frequently Asked Questions

It returns an int from 0 to 6. It is the number wrapped around a 7-day week.
get_calendar() uses it to place the first day of the month in the right column of the calendar table.
Yes. The result is still from 0 to 6. For example, -1 returns 6.
No. Most themes only call get_calendar(). Use this helper only when you build your own calendar grid.

Next: delete_get_calendar_cache()

Clear the cached calendar so get_calendar() builds a fresh one.

delete_get_calendar_cache() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful