Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Optimization

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Embedding Dynamic Dates with PHP

Static dates in articles can make articles appear stale.

Dynamic dates can be used to present the article as relevant. They can also be used to convey a sense of immediacy. Or to dynamically publish a deadline.

Using WordPress? No problem. I'll show you how to publish dynamic dates in your posts and pages. (Using my very own Insert PHP plugin.)

Because these dynamic dates use PHP code, the web page where dynamic dates are published must be a PHP page.

The date can be formatted in various ways. When using only numbers for the dates, it will work for any language. But weekday and month names are published in English.

Note: Dynamic dates on this page are UTC (the same date and time as the GMT time zone without a European Summer Time adjustment). Software uses UTC as a base for calculating local time.

Here are examples of today's date formatted in various ways:

May 9, 2024
09/May/24
May. 09, '24
09.05.24
2024-05-09
Thursday, May 9, 2024

And here's the source code of the above example. (Notes follow.)

<p style="text-align:center;">
<?php
   echo( date( 'F j, Y',    time() ) . '<br>');
   echo( date( 'd/M/y',     time() ) . '<br>');
   echo( date( 'M. d, \'y', time() ) . '<br>');
   echo( date( 'd.m.y',     time() ) . '<br>');
   echo( date( 'Y-m-d',     time() ) . '<br>');
   echo( date( 'l, F j, Y', time() ) );
?>
</p>

The two colors represent the two parameters for the date() function.

The blue color specifies the date format, how the date will be formatted. The letters represent certain parts of the date. The Willmaster date and time formatting tool can be used to generate virtually any date format you can imagine for the date() function. (To review all the letters the date() function can have, see the PHP date() function page.)

The orange color is the time() function that obtains the current date and time from the server. The next example shows how it's used to specify a date in the future relative to the current date. Simply add 86400 to time() for every day you wish to project into the future. (86400 is the number of seconds in a day.)

Here are examples for tomorrow and next week:

Tomorrow: Friday, May 10, 2024
Next week: Thursday, May 16, 2024

Here's the source code of the above example.

<p style="margin-left:1em;">
<?php
   echo('Tomorrow: '  . date( 'l, F j, Y', time()+86400     ) . '<br>');
   echo('Next week: ' . date( 'l, F j, Y', time()+(86400*7) ) );
?>
</p>

In the above source code, you'll see how 86400 is added to the value time() retrieves to get the value for tomorrow.

time()+86400

For next week, 86400 is multiplied by 7 to calculate the number of seconds in a week. The multiplication must be done before the addition, which is why that part is within parenthesis.

time()+(86400*7)

To publish a dynamic date in the past, subtract from time() instead of adding to time().

Note: If the date being published is the current date, the time() parameter is optional. Thus, the first example in this article could have dispensed with the second date parameter, specifying only the format. The second parameter was included for example completeness.

For WordPress users, pick up Insert PHP at the Willmaster.com website or at the WordPress.org website.

After installing and activating the plugin, you can put the above source code examples into your posts and pages with two changes:

  1. Replace "<?php" with "[insert_php]"
  2. Replace "?>" with "[/insert_php]"

Any date relative to now can be published dynamically on any PHP web page, including WordPress. It can make your article feel current and may even impart a sense of urgency.

(This article first appeared in Possibilities ezine.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC