Using WordPress ‘calendar_week_mod()’ PHP function

The calendar_week_mod() WordPress PHP function is used to get the number of days since the start of the week, based on the provided day number.

Usage

Let’s say you want to find out how many days have passed since the start of the week for the 10th day. You would use the calendar_week_mod() function like this:

echo calendar_week_mod(10);

The output will be 3, indicating that 3 days have passed since the start of the week.

Parameters

  • $num (int) – This is the required parameter, which represents the day number.

More information

See WordPress Developer Resources: calendar_week_mod()

Examples

Simple usage

In this example, we get the number of days since the start of the week for the 5th day.

// This will output '5'
echo calendar_week_mod(5);

Working with a variable

This example demonstrates how you can use a variable as an argument.

$dayNum = 7;
// This will output '0'
echo calendar_week_mod($dayNum);

As part of a condition

In this example, calendar_week_mod() is used in an if statement to check if it’s the start of the week.

$dayNum = 7;
if (calendar_week_mod($dayNum) == 0) {
    echo "It's the start of the week!";
}

Inside a loop

This example shows how to use the function inside a loop to get the days since the start of the week for each day in a range.

for ($i = 1; $i <= 7; $i++) {
    echo "Day $i: " . calendar_week_mod($i) . " days since the start of the week.\n";
}

With an array

This example demonstrates the use of the function with an array of day numbers.

$daysArray = array(1, 2, 3, 4, 5, 6, 7);
foreach ($daysArray as $day) {
    echo "Day $day: " . calendar_week_mod($day) . " days since the start of the week.\n";
}