skip to Main Content

What I’d like to do is display the date of the first workday of a month unless it’s past, then show the next months first workday on a WordPress website using a shortcode.

I’m thinking this could be done as a function in the WordPress Theme’s functions.php file. I have a shortcode function I’ve been using to show the current year for Copyright notices in the footer.

function year_shortcode() {
  $year = date('Y');
  return $year;
}
add_shortcode('year', 'year_shortcode');

And I have a PHP function that is supposed to show the first weekday of a month and year.

date('d-m-Y', strtotime('weekday february 2016'))

But I don’t know how to put it all together. I was thinking something like this:

  1. Get the current day, month and year variables.
  2. Put them in the PHP date above.
  3. If the resulting date is past the current date add 1 to the month and run again.
  4. Put the result in the shortcode function.

This is the first time I’ve posted on here and I’m not a programmer. Please be kind and thanks in advance for any help you can offer.

2

Answers


  1. Chosen as BEST ANSWER

    I think I figured this out with improved code and the conditional I requested in #3 above.

    function payment_date( $args ) {
        $first_weekday = date( 'm-d-Y', strtotime( 'first weekday this month' ) );
        $first_weekday_next = date( 'm-d-Y', strtotime( 'first weekday next month' ) );
        
        if($first_workday < strtotime('0:00')) {
            return $first_weekday;
        } else {
            return $first_weekday_next;
        }
    }
    add_shortcode('payment', 'payment_date');
    

    I'm using the PHP relative formats (first weekday of this month/first weekday of next month) instead of dates. Then the conditional if the result is in the past, get the first workday from the next month. Note: the 0:00 is so just the date is being compared and not the time.


  2. You can put it all together like this.

    function year_shortcode( $args ) {
        $first_workday = date('d-m-Y', strtotime('weekday'));
        return $first_workday;
    }
    add_shortcode('year', 'year_shortcode');
    

    and use this [year] shortcode anywhere.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search