How To Show Current Date and time Using Php
Heres the code.
CODE
<?php
$date = date([b]F j, o[/b]); //Displays April 25, 2007 (Assuming that is the current date)
$time = date([b]g:i a[/b]); //Displays 4:26 PM (Assuming that is the current time)
echo “Welcome! The date is $date and the time is currently $time.”;
?>
You can change the date and time formats by changing the text in bold above.
Webhosting - 0.99$/Pay monthly 1 GB Space, 5GB BW, Cpanel etcdel.icio.us Digg it Earthlink Furl iFeedReaders ma.gnolia Maple.nu Netvouz Netscape RawSugar reddit Scuttle Shadows Simpy Spurl StumbleUpon Wink Yahoo MyWeb
Rapid HTML code generation using simple PHP, avoid those repetative boring tasks….
For example, how many times do you think you have typed the following.
Example #1:
CODE
<INPUT TYPE=”TEXT” NAME=”Foo” VALUE=”Foo Value” SIZE=”25″ MAXLENGTH=”100″>
Select fields are worse, especially if you write clean code like I do with indents and seperate lines for each tag.
Example #2:
CODE
<SELECT NAME=”Fruits”>
<OPTION>Apples</SELECT>
<OPTION SELECTED>Oranges</SELECT>
<OPTION>Grapes</SELECT>
<OPTION>Peaches</SELECT>
</SELECT>
Having been writting HTML for 10 years now, so I look for as many shortcuts as possible. Now I let PHP write all of the repetitive HTML and I just fill in the blanks.
From Example #1, all we needed was the information in the quotes: “TEXT”, “Foo”,”Foo Value”,”25″,”100″
Everything else is just adding to your carpal tunnel syndrom. so lets get rid of it!
You’ll need a function:
Function #1:
CODE
<?
function build_input_field($type,$name,$value,$size,$maxlength) {
$field = “<INPUT TYPE=\”$type\” NAME=\”$name\” VALUE=\”$value\” SIZE=\”$size\” MAXLENGTH=\”$maxlength\”><BR>\n”;
return $field;
}
?>
Now call your function.
Call #1:
CODE
<? build_input_field(”TEXT”,”Foo”,”Foo Value”,”25″,”100″); ?>
The above would automatically create the code for example #1.
Now adding your own formating to the function will allow you to output code exactly the way do needed it.
Building a select field requires a little more effort. I had originally considered simply explaining the the differences between writting the code for an input field and a select field and referencing the previous Function and Call. But after some thought, I reallized that for most beginners the loops, call, arrays, and conditions needed in unison may prove to be too confusing. So I’ll expain how the entire process works for the script I wrote to build select fields from a one line call embedded in standard HTML.
Let’s revisit example #2
Example #2:
CODE
<SELECT NAME=”Fruits”>
<OPTION>Apples</SELECT>
<OPTION SELECTED>Oranges</SELECT>
<OPTION>Grapes</SELECT>
<OPTION>Peaches</SELECT>
</SELECT>
In Example #2 the required information is:
Field Name -> “Fruits”
Options -> “Apples, Oranges, Grapes, Peaches”
Selected Option -> “Oranges”
We have three main arguments for our function a four secondary arguments. for the Options argument.
Looking at the Options argument, it looks very similar to an array.
$options = array(”Apples”,”Oranges”,”Grapes”,”Peaches”);
So our call should look something like this:
Specific:
<? build_select_field(”Fruits”,array(”Apples”,”Oranges”,”Grapes”,”Peaches”),”Oranges”); ?>
General:
<? function_name(”Field_Name”,array(”Option1″,”Option2″,”Option3″,”Option4″),”Option Selected”); ?>
Now that we have a Call, we can write a function that can convert the Call into usable HTML.
Writing the function.
Step 1 - Name your function and list arguments.
function build_select_field($name,$select_array,$selected) {
Step 2 - Start writing select field code.
Simply define a variable to hold the first line of the select field code.
$select = “<SELECT NAME=\”$name\”>\n”;
Step 3 - Insert each option.
Add a FOR loop to get each value from the array in this case four different fruits. Use the count() function to find out the total number of values in the array then execute the code below for each value.
for ($x = 0; $x < count($select_array); $x++) {
Step 4 - Get the selected value.
Use an IF statement to determine if the current value from the array is the value that should be selected by default.
if ($select_array[$x] == $selected) {
Step 5 - Write the <OPTION SELECTED> code.
Just add the new HTML to the previous information contained in $select using ” .= ” instead of ” = ” only. Don’t forget to close the IF statement with ” } ”
$select .= “<OPTION SELECTED>$select_array[$x]</OPTION>\n”;
}
Step 6 - Write the <OPTION> code.
Use an ELSE statement to write the non-selected option code again adding the new HTML to the old with ” .= ” and not ” = “. Don’t forget to close the ELSE statement with ” } ”
else {
$select .= “<OPTION>$select_array[$x]</OPTION>\n”;
}
Step 7 - Finish up.
Close the FOR loop with ” } “, close the Select tag with </SELECT>, Return a value, and close the function with ” } “.
}
$select .= “</SELECT><BR>\n”;
return $select;
}
The final code should look like the following:
function build_select_field()
CODE
function build_select_field($name,$select_array,$selected) {
$select = “<SELECT NAME=\”$name\”>\n”;
for ($x = 0; $x < count($select_array); $x++) {
if ($select_array[$x] == $selected) {
$select .= “<OPTION SELECTED>$select_array[$x]</OPTION>\n”;
}
else {
$select .= “<OPTION>$select_array[$x]</OPTION>\n”;
}
}
$select .= “</SELECT><BR>\n”;
return $select;
}
So this would be quite silly to use if you need only one or two select fields unless you place your function in it’s own file and include that file’s contents in pages that use forms. But if you have several select fields and/or many options in each field, this could save you a lot of time.
With a little more PHP or a modified call, the build_select_field() function can build a select field from any array including databases like MySQL.
Hope this helps cut your work load coding forms.
This same technique can be applied to Tables as well but I love the idea for headers and footers. Every page in you website can have the same appearence by simply writing a code for all of the pages HTML code except that which is specific to that page. That way your format would be uniform while the content changes.
Happy coding.
Webhosting - 0.99$/Pay monthly 1 GB Space, 5GB BW, Cpanel etcdel.icio.us Digg it Earthlink Furl iFeedReaders ma.gnolia Maple.nu Netvouz Netscape RawSugar reddit Scuttle Shadows Simpy Spurl StumbleUpon Wink Yahoo MyWeb « Previous Entries















