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:
November 21, 2024
21/Nov/24
Nov. 21, '24
21.11.24
2024-11-21
Thursday, November 21, 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, November 22, 2024
Next week: Thursday, November 28, 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:
- Replace "<?php" with "[insert_php]"
- 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