Example 144: Converting a Date from User Input
Description: This WebSmart PHP example converts a date input by the user into the format YYYY/MM/DD.
-
Open a new 'Simple Page' template.
-
Add this code near the top of PHP source-code:
global $from_date, $to_date;
if (isset($_REQUEST['date']))
{
$from_date = $_REQUEST['date'];
}
-
Add this task to the PHP source-code:
if ($pf_task == 'convert_date')
convert_date();
-
Add this function to the PHP source-code:
function convert_date()
{
// Make all global variables available here
foreach($GLOBALS as $arraykey=>$arrayvalue)
{
global $$arraykey;
}
if( strtotime($from_date) ){
$to_date = strftime("%Y/%m/%d", strtotime($from_date));
}
else{
$to_date = "Not a valid date format";
}
wrtseg('MainSeg');
}
-
Add this html to the main segment:
<form name="val_date" action="$pf_scriptname" method="POST">
<input type="hidden" name="task" value="convert_date">
<table>
<tr>
<td>Enter date:</td>
<td><input type="text" name="date" value="" size="35"></td>
<td><input type="submit" value="Submit "></td>
</tr>
</table>
</form>
<div style="color:red; margin-left: 100px; text-style:bold;"><b>$to_date</b></div>
|