PHP - Generate paycheck payment dates

By xngo on August 4, 2019

Like most people, your paychecks come either biweekly or semimonthly. Regardless of your payout frequencies, money is transferred to your bank account on the 1st day of the month, the middle of the month or the last day of the month. But these days need to be a business day. If 1 of these days is not a business day, then the last previous business day is used. In this tutorial, I will show you how to generate payout dates using PHP.

Get payout date on a business day

First, let's implement a method to return the payout date on a business day. If the payout date is on the weekends, then Friday is used. Here is the code.

<?php
 
function getValidBusinessDate($dt) {
 
    // Get the day of week.
    $timestamp = $dt->getTimestamp();
    $dw = date( 'N', $timestamp);   // 1=Monday, 7=Sunday
 
    // If day of week is Sat or Sun, then return Fri.
    $days_to_subsract = $dw - 5;
    if ( $days_to_subsract > 0 ) {
        $interval = 'P' . $days_to_subsract . 'D';
        $d = $dt->sub( new DateInterval($interval) );
        return $d->format('Y-m-d');
    }
    else {
        return $dt->format('Y-m-d');
    }
}
 
# Example using getValidBusinessDate($dt).
$dt = new DateTime();
$dt->setDate(2019, 8, 4);
echo getValidBusinessDate($dt);    // Output for Friday: 2019-08-02
 
?>

Get last day of the month

Now, let's implement a method to return the last day of the month. Here is the code.

<?php
 
function getLastDayOfMonth($dt) {
    // modify() uses the same format as strtotime().
    return $dt->modify('last day of this month');
}
 
# Example using getLastDayOfMonth($dt).
$dt = new DateTime();       // Create an instance of the current date & time.
$dt->setDate(2019, 8, 4);   // Set $dt to a different date.
 
$last_day_of_month = getLastDayOfMonth($dt);
echo $last_day_of_month->format('Y-m-d');   // Output: 2019-08-31
 
?>

Generate payout date for mid and last day of month

Finally, let's put everything together and generate payout date for the middle and the last day of the month for the whole year. Here is the code.

<?php
 
function getValidBusinessDate($dt) {
 
    // Get the day of week.
    $timestamp = $dt->getTimestamp();
    $dw = date( 'N', $timestamp);   // 1=Monday, 7=Sunday
 
    // If day of week is Sat or Sun, then return Fri.
    $days_to_subsract = $dw - 5;
    if ( $days_to_subsract > 0 ) {
        $interval = 'P' . $days_to_subsract . 'D';
        $d = $dt->sub( new DateInterval($interval) );
        return $d->format('Y-m-d');
    }
    else {
        return $dt->format('Y-m-d');
    }
}
 
function getLastDayOfMonth($dt) {
    return $dt->modify('last day of this month');
}
 
# Generate payout date for 2019.
# -----------------------------------
$year = 2019;
$dt = new DateTime();
 
for($m=1; $m<=12; $m++) {
 
    // Get the middle of the month date.
    $dt->setDate($year, $m, 15);
    $mid_month = getValidBusinessDate($dt);
 
    // Get the last day of the month date.
    $last_day_of_month = getValidBusinessDate( getLastDayOfMonth($dt) );
 
    echo "$mid_month | $last_day_of_month\n";
}
 
?>

Output

2019-01-15 | 2019-01-31
2019-02-15 | 2019-02-28
2019-03-15 | 2019-03-29
2019-04-15 | 2019-04-30
2019-05-15 | 2019-05-31
2019-06-14 | 2019-06-28
2019-07-15 | 2019-07-31
2019-08-15 | 2019-08-30
2019-09-13 | 2019-09-30
2019-10-15 | 2019-10-31
2019-11-15 | 2019-11-29
2019-12-13 | 2019-12-31

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.