Dynamic Date Display

I need to display a dynamic date on my site, but I'm not sure the best way to do it. If I could automate this process it would be very time-saving in the long run.

I need to display a specific date, that should change dynamically on the 10th day of every month.

For example, if today is January 4, the date should read "February 1" but on January 10, the date should read "March 1" and it will stay that way until February 10, when it will become "April 1" and so on.

Is such a thing possible? Thanks!

  • Adam Czajczyk

    Hello Christopher,

    I hope you're well today and thank you for your question!

    I believe that should be doable and I'll be happy to help you with that but I think I'd like to get a better picture of your goal first :slight_smile:

    Please replay to these questions:

    1. I understand that this should be "just date" so it could for example be a shortcode that you can put anywhere on the site, is that correct? Or do you mean the post/page publication date and/or it should be somehow connect to some sort of content of your site?

    2. Apart from the fact that the date should change on every 10th of each month I'm not quite sure what would be the "algorithm"/"scheme", I mean: what's the relationship between these dates? Could you please explain it in a bit more detailed way so I could understand how those changes should work?

    I hope I'll then be able to assist you with this :slight_smile:

    Best regards,
    Adam

  • Christopher

    Hi Adam,

    To answer your questions:

    1. I understand that this should be "just date" so it could for example be a shortcode that you can put anywhere on the site, is that correct? Yes, I'm looking just to have a shortcode to display text.

    2. Apart from the fact that the date should change on every 10th of each month I'm not quite sure what would be the "algorithm"/"scheme", I mean: what's the relationship between these dates? Could you please explain it in a bit more detailed way so I could understand how those changes should work? Really, the "changing the date on the 10th of each month" is the primary purpose here. The date will always be "[Month] 1st, [Year]" and what I need is for that [month] and [year] to advance one month forward on the 10th of each month.

    Some examples:
    - Actual Date: January 9, 2017. Shortcode Displays Date: February 1st, 2017
    - Actual Date: January 10, 2017. Shortcode Displays Date: March 1st, 2017
    - Actual Date: February 1, 2017. Shortcode Displays Date: March 1st, 2017
    - Actual Date: February 9, 2017. Shortcode Displays Date: March 1st, 2017
    - Actual Date: February 10, 2017. Shortcode Displays Date: April 1st, 2017

    The date will always advance 1 month on the 10th of the month.

    ...and so on. Does this provide some extra clarity?

  • Adam Czajczyk

    Hello Christopher!

    That makes things clear, thank you :slight_smile:

    I think this code should do the trick:

    function wpmu_fixed_date_shortcode() {
    	$curr_day = date('j');
    	$curr_month = date('F');
    	$curr_year = date('Y');
    	if ($curr_day >= 10) {
    		$curr_day = "1st";
    		$curr_month = date('F', strtotime("+1 month", time()));
    		if ((date('n')+1) == 1) $curr_year = date('Y')+1;
    	}
    	$my_date = $curr_month . " " . $curr_day . ", " . $curr_year;
    	return $my_date;
    }
    add_shortcode('show-set-date', 'wpmu_fixed_date_shortcode');

    In order to apply it to your site add it to your theme's (preferably the child theme) "functions.php" file and then use

    [show-set-date]

    shortcode anywhere in your posts/pages.

    I hope that helps :slight_smile:

    Best regards,
    Adam

  • Adam Czajczyk

    Hello Christopher!

    I checked the code and I noticed that I only addressed the case when it's 10th or later day of the month but not when it's "before 10th". I'm sorry about that.

    Here's an updated code, I think that should be working as expected now:

    function wpmu_fixed_date_shortcode() {
    	$curr_day = date('j');
    	$curr_month = date('F');
    	$curr_year = date('Y');
    	if ($curr_day >= 10) {
    		$curr_day = "1st";
    		$curr_month = date('F', strtotime("+2 month", time()));
    		if ((date('n')+2) == 1) $curr_year = date('Y')+1;
    	}
    	else {
    		$curr_day = "1st";
    		$curr_month = date('F', strtotime("+1 month", time()));
    		if ((date('n')+1) == 1) $curr_year = date('Y')+1;
    	}
    	$my_date = $curr_month . " " . $curr_day . ", " . $curr_year;
    	return $my_date;
    }
    add_shortcode('show-set-date', 'wpmu_fixed_date_shortcode');

    Best regards,
    Adam

  • Christopher

    Adam, this works beautifully! Thank you for your effort, I really appreciate it!

    If it's not too much to ask, I have a very similar need on the same website, and you seem like the best person to help with this!

    I'm looking to make a different shortcode that is also displays a date that changes, but this one is quarterly instead of monthly.

    The dates to display are the first day of the next quarter, always one of these 4 options:
    - January 1, [Year]
    - April 1, [Year]
    - July 1, [Year]
    - October 1, [Year]

    Just like with the month-based script, I would be looking to have the displayed date to change on the 10th of the month (in this case, the last month of the quarter).

    Some examples:
    - Actual Date: January 8, 2017. Shortcode Displays Date: April 1st, 2017
    - Actual Date: February 8, 2017. Shortcode Displays Date: April 1st, 2017
    - Actual Date: March 8, 2017. Shortcode Displays Date: April 1st, 2017
    - Actual Date: March 10, 2017. Shortcode Displays Date: July 1st, 2017
    - Actual Date: April 2, 2017. Shortcode Displays Date: July 1st, 2017

    This means that the displayed quarter will change on the following dates: March 10, June 10, September 10, and December 10.

    Is this something you might be able to help me out with?

    Thank you!!!

  • Adam Czajczyk

    Hello Christopher!

    Here's another piece of code for you:

    function wpmu_quarter_date_shortcode() {
    
    	$curr_day = date('j');
    	$curr_month = date('n');
    	$change_months = array('3','6','9','12');
    
    	if ( ($curr_day>=10) AND (in_array($curr_month,$change_months)) ) {
    		if ($curr_month<4) { $curr_month = "July"; }
    		elseif ($curr_month<7) { $curr_month = "October"; }
    		elseif ($curr_month<10) { $curr_month = "January"; }
    		else { $curr_month = "April"; }
    	}
    	else {
    		if ($curr_month<4) { $curr_month = "April"; }
    		elseif ($curr_month<7) { $curr_month = "July"; }
    		elseif ($curr_month<10) { $curr_month = "October"; }
    		else { $curr_month = "January"; }
    	}
    
    	if ($curr_month == "January") $curr_year = date('Y', strtotime("+1 year", time()));
    	else $curr_year = date('Y');
    
    	$curr_day = "1st";
    
    	$my_date = $curr_month . " " . $curr_day . ", " . $curr_year;
    
    	return $my_date;
    }
    add_shortcode('show-quarter-date', 'wpmu_quarter_date_shortcode');

    The shortcode to use would be [show-quarter-date] and I think it's working fine. However, I'm not 100% sure if I didn't make any "logic" mistake here so you would want to test it (you would most likely need to play with a date on server a bit).

    Hopefully though, it should be fine. Please note however that if you find a "flow" in logic with how this code works, I can try to adjust it but that'd be as far as I can go with that as it's a custom solution.

    In case you'd need more advanced/dedicated code, please consider asking for it on our "Jobs & Pros" job board (please note: no WPMU DEV staff involved!) here:

    https://premium.wpmudev.org/wordpress-development/

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.