PHP stores time in a form called a timestamp.. A timestamp is measured in seconds starting from January 1, 1970 00:00:00 GMT. This makes it easy for computers to calculate the difference between times; just subtract one from another, but it is not so easy for humans to use. To generate a timestamp, use the function time():
<?
echo time();
?>
Remember that time() will get the time from the server, not the browser of the person viewing the web page. Using the code above give the following timestamp for when you accessed this web page:
1328350699
To convert time() to a more human friendly format, you can use getdate(). getdate() has one optional argument; a timestamp. If used without an argument, getdate will use the current timestamp. getdate returns an associative array containg the following elements:
| Key | Description | Example |
|---|---|---|
| seconds | Seconds past the minute | 19 |
| minutes | Minutes past the hour | 18 |
| hours | Hours of the day | 5 |
| mday | Day of the month (1-31) | 4 |
| wday | Day of the week (0-6) | 6 |
| weekday | Day of the week. (name) | Saturday |
| yday | Day of the year (0-365) | 34 |
| mon | Month (1-12) | 2 |
| month | Month (name) | February |
| year | Year (4 digits) | 2012 |
| weekday | Day of the week (name) | Saturday |
| 0 | Timestamp | 1328350699 |
Here is how to use getdate() to display today's date:
<?
$today = getdate();
echo "Today is " . $today[mon] . "/" . $today[mday] . "/" . $today[year];
?>
You can use getdate() to make a web page automatically change themes for different months. Here is how I change the theme for http://danceawaysb.com/index.php:
<?
$now = getdate();
$month = $now[mon];
if ( $month = 10 )
{ $style = \"css-oct.txt";
$splash = "splash-oct.txt";
$bar = "<img src=\"images/blooddrip.gif\" width=\"532\" height=\"50\" alt=\"=====================================\">";
}
elseif ( $month = 12)
{
$style = "css-dec.txt";
$splash = "splash-dec.txt";
$bar = "<img src=\"images/xholly.gif\" height=\"72\" width=\"500\" alt=\"=====================================\">";
}
else
{ $style = "css-normal.txt";
$splash = "splash-normal.txt";
$bar = "<img src=\"images/multiline5.gif\" width=\"544\" height=\"15\" alt=\"=====================================\">";
}
</p>
The style sheet and the block of images at the top are now string variables. The variables for the CSS & the splash are used in PHP includes. The divider bars are also string variables and are echoed. You can see the code for the whole page here.
|
|
|