Chapter 17:
Creating Web Pages With PHP

Google

How to create a web page: Start with HTML

I'm using the echo() function to display the time and date on the page. The time may not be the same as your local time. That's because your web server might not be in the same time zone as you are. Suppose you're in the Eastern time zone and the server is in the Pacific time zone. The time shown on the page will be three hours earlier than your time.

You can format the date and time in many different ways by using different options with the date() function. Refer to the following table for an explanation of each one. Notice that the "S" option is designed to be combined with month. In our example above, dS means show the day of the month with leading zeros (01, 02, 03,...) and show the ordinal suffix (1st, 2nd, 3rd, 4th...).

CodeDescription
aam or pm
AAM or PM
BSwatch Beat time
dDay of the month with leading zeroes
DDay of the week with 3-letter abbreviations
FName of the month
hHour from 01 to 12
HHour from 00 to 23
gHour from 1 to 12 (no leading zeroes)
GHour from 0 to 23 (no leading zeroes)
iMinutes
jDay of the month with no leading zeroes
lDay of the week
L1 if leap year, 0 otherwise
mMonth number from 01 to 12
MAbbreviated month name (Jan., Feb, etc.)
nMonth number from 1 to 12 (no leading zeroes)
sSeconds from 00 to 59
SOrdinal suffix for day of the month (1st,2nd,3rd)
tNumber of days in the month
USeconds since the epoch
yYear as two digits
YYear as four digits
zDay of the year from 0 to 365
ZTimezone offset in seconds (-43,200 to 43,200)

Automatic expiration date for an offer

Suppose you want to give a person exactly 48 hours (or whatever time frame you choose) to respond to an offer. If they make their purchase by the deadline you could give them a reduced price or extra bonuses. First, you need to calculate the end of the deadline period. Here's how:

<?php
$hours = 48;
$exp = date("Y-m-d h:i:s", mktime(date("h")+($hours)));
?>

Once you've included the little PHP block above which determines the expiration date and time, you put your deadline notice into the sales page like this:

This offer expires at <?php echo "$exp";?> EST.

How does it work? The date() function determines the current time. Remember that the hour will depend on the timezone your server is in. Then the mktime() function calculates what the time will be exactly 48 hours from the time the page was displayed.

You can set the value of $hours to be 24 hours, 72 hours or even several weeks later. Remember to be fair to all customers. Make sure that the deadline display shows the same time zone as the server so that the expiration will be exactly right.

Want to calculate how many hours there are in "x" number of days? Here's the code I use:

$number_of_days = "x";
$hours_per_day = 24;
$hours = $number_of_days * $hours_per_day;

Putting it all together and making it a function would look like this:

<?php
function expires ($x) {
  $number_of_days = $x;
  $hours_per_day = 24;
  $hours = $number_of_days * $hours_per_day;
  $when = date("Y-m-d h:i:s", mktime(date("h")+($hours)));
  return $when;
}
?>

To calculate an expiration date or any other future date and time, just call your new expires() function and pass it a number of days (2, in this case) like this:

$exp = expires(2);

You could use this function for any number of things besides an offer expiration date. It could be used to show people exactly when their trial period or guarantee ends.

Now you have a general-purpose expiration time and date generator. You just set the desired number of days by changing the 2 in the call to the expires() function to the number of days you want. The line to paste into your web page is still:

This offer expires at <?php echo "$exp";?> EST.

Here's another cool thing to know. Since you have the exact date and time that the offer expires, you can put it into an email that you can send to a prospect. If you tease them with a freebie, you'll want to email it to them. That way you'll have their email address because they had to give it to you in order to get the freebie. Remind them about your offer and its expiration date in the message you send out. Don't forget to personalize the email message, too!

What about enforcing the deadline strictly? It's possible to record the time the offer was made, along with an IP address, referring page, name, and email address in a database. If the prospect does decide to buy your product, you can check the database to see if they met the deadline. What you do with that info is up to you.

Previous Page   Table of Contents   Next Page

Copyright © 2004 Steve Humphrey