| px | top | add code | search | signup | login | help |
<?
###################################################################
#
# htmlDateDraw.php3
#
# this is a php3 library that contains some useful date functions
#
# prettyDate(int)
# just a wrapper for the built in date function
# that returns a string that is a pretty american
# representation of a UNIX time's MONTH/DAY/YEAR
#
# prettyTime(int)
# just a wrapper for the built in date function
# that returns a string that is a pretty american
# representation of a UNIX time's HOUR:MINUTE AM/PM
#
# prettyDateTime(int)
# combines the two above wrappers
#
# drawDate(string $name, int, $date, int $mode)
# draws the datetime represented by the UNIX time $date
# in an HTML form referenced by $name. read back into
# a UNIX time with below readDate
# $mode can be:
# 1 - just the date will be drawn
# 2 - just the time will be drawn
# 3 - both date and time will be drawn
#
# readDate(string $name, assoc_array $form)
# returns the UNIX time retrived from the form $form
# with the name $name. put the date in the form with
# above drawDate
#
# bool existsDate(string $name, assoc_array $form)
# retruns true if the date named $name was drawn into
# the submitted form $form
#
#
# Use this library as you see fit.
#
# http://tjw.org/projects
#
# 1999-04-02 Tony J. White tjw@tjw.org
#
###################################################################
//####
//# draws the date readable to americans
function prettyDate($unixtime) {
return date("m/d/Y", $unixtime);
}
//####
//# draws the time readable to americans
function prettyTime($unixtime) {
return date("h:i A", $unixtime);
}
//####
//# combines the two above functions for ease of use
//# in some circumstances
function prettyDateTime($unixtime) {
return prettyDate($unixtime)." ".prettyTime($unixtime);
}
//####
//#
//# draws a UNIX time in HTML form format to be interpreted by
//# the following function "readDate(string, array)"
//# $name is how the variable will be referenced using
//# readDate(string, array)
//# $date is the UNIX time
//# $mode is how it should be drawn
//# 1 - just date
//# 2 - just time
//# 3 - both date and time
//#
function drawDate($name, $date, $mode) {
?>
<input type=hidden name="<?echo $name;?>Exists" value="true">
<table border=0 cellspacing=0 cellpadding=0>
<?
if ($mode == 1 || $mode == 3) {
// year contained in $date
$year = date("Y", $date);
// five years before $date
$startYear = date("Y", ($date-(60*60*24*365*5)));
// ten years from now
$stopYear = date("Y", (time()+(60*60*24*365*10)));
// month contained in $date
$month = date("m", $date);
// day contained in $date
$day = date("d", $date);
?>
<tr>
<td>
<table border=0 cellspacing=0 cellpadding=0>
<tr>
<td>
<select name="<?echo $name;?>Month">
<?
// the month pull down menu
for ($i = 1; $i <= 12; $i++) {
$tempTime = mktime(0,0,0,$i,1,1970);
$tempMonth = date("m", $tempTime);
$tempMonthName = date("F", $tempTime);
?>
<option value="<?echo $tempMonth;?>"<?
if ($month == $tempMonth) echo " selected";
?>><?echo $tempMonthName;?>
<?
}
?>
</select>
</td>
<td>
<select name="<?echo $name;?>Day">
<?
// the day pull down menu
for ($i = 1; $i <= 31; $i++) {
?>
<option value="<?echo $i;?>"<?
if ($day == $i) echo " selected";
?>><?echo $i;?>
<?
}
?>
</select>
</td>
<td>
<select name="<?echo $name;?>Year">
<?
for ($i = $startYear; $i <= $stopYear; $i++) {
?>
<option value="<?echo $i;?>"<?
if ($year == $i) echo " selected";
?>><?echo $i;?>
<?
}
?>
</select>
</td>
</tr>
</table>
</td>
</tr>
<?
// this is the end of the actual DATE part of the function
// now draw the TIME if the $mode calls for it
}
if ($mode == 2 || $mode == 3) {
$hour = date("h", $date);
$minute = date("i", $date);
$amPm = date("A", $date);
?>
<tr>
<td>
<table border=0 cellspacing=0 cellpadding=0>
<tr>
<td>
<select name="<?echo $name;?>Hour">
<?
// the hour pull down menu
for ($i = 1; $i <= 12; $i++) {
?>
<option value="<?echo $i;?>"<?
if ($hour == $i) echo " selected";
?>><?echo $i;?>
<?
}
?>
</select>
</td>
<td>
<select name="<?echo $name;?>Minute">
<?
// the minute pull down menu
for ($i = 0; $i <= 59; $i++) {
?>
<option value="<?echo $i;?>"<?
if ($minute == $i) echo " selected";
?>>
<?
if ($i < 10) echo "0";
echo $i;
?>
<?
}
?>
</select>
</td>
<td>
<select name="<?echo $name;?>AMPM">
<?
if ($amPm == "AM") echo "<option selected>AM<option>PM";
else echo "<option>AM<option selected>PM";
?>
</select>
</td>
</tr>
</table>
</td>
</tr>
<?
}
?>
</table>
<?
}
//####
//#
//# returns true if there drawDate has drawn a date into the form $form
//# named $name. false otherwise
//#
function existsDate($name, $form) {
return($form[$name."Exists"] == "true");
}
//####
//#
//# returns the UNIX time written into the form $form by
//# drawDate(string, int, int)
//#
function readDate($name, $form) {
// return blank instead of 0 just in case
// existsDate was not used beforehand
if (! existsDate($name, $form)) return "";
// mktime takes care of almost everything
// all we need to do is find the 24hr hour
// value
// build hour
$amPm = $form[$name."AMPM"];
$hour = $form[$name."Hour"];
if ($amPm == "AM" && $hour == 12) $hour = 0;
if ($amPm == "PM") $hour = $hour + 12;
$hour = intval($hour);
$minute = intval($form[$name."Minute"]);
$month = intval($form[$name."Month"]);
// in the case of mode 2 we need to fudge the month
if ($month == 0) $month = 1;
$day = intval($form[$name."Day"]);
// in case of mode 2 we need to fudge the day
if ($day == 0) $day = 1;
$year = intval($form[$name."Year"]);
// in the case of mode 2, we need to fudge the year
if($year == 0) $year = 1970; //unix epoch
// go to town, mktime
return mktime($hour,
$minute,
0,
$month,
$day,
$year);
}
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.