|
Sneaky Autodate With PHP
In my "trick" about "Autodate",
I showed how you can display the current date on a
web page using a bit of JavaScript. This works well,
but savvy web visitors might just have a look at the
source code for your web page and realise that it's
not really a genuine current date that is hard-coded
into the web page.
But if you use "PHP", you can fool
even these web-savvy visitors.
PHP is a very powerful
web scripting language. Extremely complex websites
have been built using this language. But it is also
possible to take tiny snippets of PHP code and use
them to enhance your website.
The only restriction is that any
page you build which has PHP code in it must end
with the ".php" file type, not ".html" or ".htm".
OK, there are ways around this, but that's another
story. But most web hosting accounts (including my Hosting4Marketing web
space packages) support PHP.
So, how do you code the current
date in PHP? It's really very simple. Here's the code:
<?php
$today = date('F j, Y');
PRINT "$today";
?>
If someone looks at the source
code of your page, they won't see the code - just the
current date. Here's the proof:
Today's date is July 31, 2010
.. and that's what you'll see if
you look at the source code of this page.
You can do all sorts with this
code snippet. You can, for example, change the parameters
to show the date in a different format. In the above
example, the "F" displays the current month, the "j",
the day of the month and the "Y", the year (4 figure
format. So here's another example of the code:
<?php
$today = date('l, j M, y');
PRINT "$today";
?>
This is a different date
format: Saturday, 31 Jul, 10
See if you can work out what the
new parameters represent :-)
You can even set the date
to show a set number of days in advance:
<?php
$today = date('F j, Y',strtotime("+4 days"));
PRINT "$today";
?>
That displays the date 4 days from
now (just change the figure in red to what ever you
like. Here's the proof:
In 4 days time, it will
be August 4, 2010
There are a lot of different parameters
associated with the PHP "date" function and you
can get a full list of all the parameters here:
http://www.php.net/date
When you insert these code snippets
into your web pages, make sure that you do this in
HTML mode if you're using a WYSIWYG web page editor
(such as FrontPage, Dreamweaver or NVU). What I normally
do is to type the words surrounding the code in WYSIWYG
mode, and then put "xxx" in as a place marker,
such as
I will be raising the price
on xxx so buy today
I then switch to the HTML mode
and replace the "xxx" with the PHP code.
Warning: When
I make a special offer to my subscribers, I do not use
this technique. It's a real date that I set and
I keep to it!
I hope you've found this little
"trick" useful. Enjoy!
|