INTRODUCTION
LESSONS
MORE PHP
|
04 - Dates and Times Using PHP
1283806339 seconds have elapsed since the Unix Epoch (January 1 1970 00:00:00 GMT).
Hour = 20
Minutes = 52
Seconds = 19
Day = 6
Month = 8 (Jan = 0)
It is September
Year = 2010
Weekday = 1
Today is Monday
Yearday = 248
Daylight saving is not in effect.
Homework: Create
a web page that greets you with "Good Morning", "Good Afternoon"
Etc. depending on the time of day.
<!-- Here is the page source code for 04date.php -->
<?php
include "code.php"; // Contains the function used to display
// the source code of web pages.
// =======================================
function nameOfDay($aDay)
{
switch ($aDay)
{
case 0 : $theDay = "Sunday"; break;
case 1 : $theDay = "Monday"; break;
case 2 : $theDay = "Tuesday"; break;
case 3 : $theDay = "Wednesday"; break;
case 4 : $theDay = "Thursday"; break;
case 5 : $theDay = "Friday"; break;
case 6 : $theDay = "Saturday"; break;
default : $theDay = "Invalid Day";
}
return $theDay;
}
// =======================================
function nameOfMonth($aMonth)
{
switch ($aMonth)
{
case 0 : $theDay = "January"; break;
case 1 : $theDay = "February"; break;
case 2 : $theDay = "March"; break;
case 3 : $theDay = "April"; break;
case 4 : $theDay = "May"; break;
case 5 : $theDay = "June"; break;
case 6 : $theDay = "July"; break;
case 7 : $theDay = "August"; break;
case 8 : $theDay = "September"; break;
case 9 : $theDay = "October"; break;
case 10 : $theDay = "November"; break;
case 11 : $theDay = "December"; break;
default : $theDay = "Invalid Month";
}
return $theDay;
}
// =======================================
$theTime = time();
$timeArray = localtime($theTime, 1);
$hour = $timeArray['tm_hour'];
$min = $timeArray['tm_min'];
$sec = $timeArray['tm_sec'];
$day = $timeArray['tm_mday'];
$month = $timeArray['tm_mon'] ;
$monthName = nameOfMonth($month);
$year = $timeArray['tm_year'] + 1900;
$wday = $timeArray['tm_wday'];
$dayName = nameOfDay($wday);
$yday = $timeArray['tm_yday'];
$tm_isdst = $timeArray['tm_isdst'];
if ($tm_isdst)
{
$daylight = "<p>Daylight saving is in effect.</p>\n";
}
else
{
$daylight = "<p>Daylight saving is not in effect.</p>\n";
}
?>
<html>
<head>
<title>Dates and Times Using PHP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<table border="1" cellpadding="4" cellspacing="0">
<tr valign="top">
<td><?php include "nav.php"; ?></td>
<td>
<h1>04 - Dates and Times Using PHP</h1>
<?php
echo "<p>$theTime seconds have elapsed since the Unix ".
"Epoch (January 1 1970 00:00:00 GMT).</p>\n";
echo "<p>Hour = $hour</p>\n";
echo "<p>Minutes = $min</p>\n";
echo "<p>Seconds = $sec</p>\n";
echo "<p>Day = $day</p>\n";
echo "<p>Month = $month (Jan = 0)</p>\n";
echo "<p>It is $monthName</p>\n";
echo "<p>Year = $year</p>\n";
echo "<p>Weekday = $wday</p>\n";
echo "<p>Today is $dayName</p>\n";
echo "<p>Yearday = $yday</p>\n";
echo "$daylight\n";
?>
<p><strong><font color="#FF0000">Homework:</font></strong> Create
a web page that greets you with "Good Morning", "Good Afternoon"
Etc. depending on the time of day.</p>
<?php
dump_page("04date.php");
?>
</td>
</tr>
</table>
</body>
</html>
|
|