note 18659 added to function.strtotime
| From: | mfisch at rmtoemail dot kaz dot com | Date: | Wed, 30 Jan 2002 21:30:37 +0000 |
| Subject: | note 18659 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-26046@lists.php.net to get a copy of this message | ||
When confronted with the task of creating a user interface for choosing UNIX timestamps I used
strtotime() to create a few reusable functions. I use timestamps all over my SQL databases as
document keys, hope someone else finds this usefull.
Notes:
date_select_form() could use an optional switch to turn on hour/minute selections.
-------------------date_select.php------------------------------------
<?php
function date_select_form ($fn, $startyear)
{
# date_select_form(str varbase, int year)
# ``varbase'' is the base name of the variable, it must be specified to allow for
multiple instances of date_select_form()
# The year select options are from ``year'' through current system year.
$months = array(1 => 'January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December');
$lfn = $GLOBALS["$fn"];
$ascvar = $fn . "[1]";
$ret.=" <select name=\"$ascvar\">\n";
$cur = 1; while ( $cur <= 12 )
{
if ( $lfn[1] == $cur ) { $selected = " SELECTED"; }
$ret.=" <option value=$cur$selected>($cur) $months[$cur]</option>\n";
$cur++; $selected = "";
}
$ret.=" </select>\n";
$ascvar = $fn . "[2]";
$ret.=" <select name=\"$ascvar\">\n";
$cur = 1; while ( $cur <= 31 )
{
if ( $lfn[2] == $cur ) { $selected = " SELECTED"; }
$ret.=" <option value=$cur$selected>$cur</option>\n";
$cur++; $selected = "";
}
$ret.=" </select>\n";
$ascvar = $fn . "[0]";
$ret.=" <select name=\"$ascvar\">\n";
$cur = $startyear; while ( $cur <= date("Y") )
{
if ( $lfn[0] == $cur ) { $selected = " SELECTED"; }
$ret.=" <option value=$cur$selected>$cur</option>\n";
$cur++; $selected = "";
}
$ret.=" </select>";
return $ret;
}
function date_select_proc ($fn)
{
# date_select_proc(str varbase)
# varbase is the base name of the variable set used previously in date_select_form()
# date_select_proc() returns -1 on failure to find global get/post input variables
$lfn = $GLOBALS["$fn"];
$strdate = "$lfn[0]-$lfn[1]-$lfn[2]";
if ( $strdate != "--" )
{
$timestamp = strtotime($strdate);
return $timestamp;
} else {
return -1;
}
}
?>
<h2>date_select_*() test page</h2>
<hr>
<?
$timestamp = date_select_proc("test");
$timestamp2 = date_select_proc("test2");
if ( $timestamp > 0 )
{
print "Found range $timestamp - $timestamp2.<hr>";
}
print "<form method=get action=date_select.php>\n";
print "Date Range<br>";
print date_select_form("test", 2001);
print "<br>\n";
print date_select_form("test2", 2001);
print "<br>\n";
print "<input type=submit value=\"Convert to Timestamp Range\">\n";
print "</form>";
?>
--
http://www.php.net/manual/en/function.strtotime.php
http://master.php.net/manage/user-notes.php?action=edit+18659
http://master.php.net/manage/user-notes.php?action=delete+18659
http://master.php.net/manage/user-notes.php?action=reject+18659