Using date() to display time

The date function can also be used to display time too.

Hours
h – 12-hour 01 to 12
g – 12-hour 1 to 12
H – 24-hour 00 to 24
G – 24-hour 0 to 23
Minutes
i – 00 to 59
Seconds
s – 00 to 59
AM or PM
A – Uppercase
a – Lowercase

Here’s an example:

<?php
echo date( "h:i:s A" ) ;
?>

Posted in Code, PHP

PHP Date Function

I’ve created a PHP page that shows the different types of output you can get from the date() function here. This page doesn’t utilize all of the many options this function offers because I was trying to just use the most common ones. The main thing to remember about this is that since this is PHP your output is going to be the date on your server.

Today is:</br>
<?php echo date("l"Smilie: ;); ?></br>
<?php echo date("D"Smilie: ;); ?></br>

</br>The</br>
<?php echo date("d"Smilie: ;); ?></br>
<?php echo date("j"Smilie: ;); ?></br>
<?php echo date("jS"Smilie: ;); ?></br>

</br>The month is:</br>
<?php echo date("F"Smilie: ;); ?></br>
<?php echo date("M"Smilie: ;); ?></br>
<?php echo date("m"Smilie: ;); ?></br>
<?php echo date("n"Smilie: ;); ?></br>

</br>The year is:</br>
<?php echo date("Y"Smilie: ;); ?></br>
<?php echo date("y"Smilie: ;); ?></br>

</br>The date is:</br>
<?php echo date("d M Y"Smilie: ;); ?> - Military style</br>
<?php echo date("l"Smilie: ;)." the ".date("jS \of F Y"Smilie: ;); ?> - Long written</br>
<?php echo date("m/d/y"Smilie: ;); ?> - Short</br>

If you’re wondering why I used so many php opening tags I did that I could come back later and copy and paste some of these. This was probably overkill because if I was going to recycle any code from this it would be from the last three lines. Oh well, at least the output looks ok.

Posted in Code, PHP

Hello Variable

Here’s a simple PHP program I’ve made to make Hello World! a little more interactive.


Hello PHP Example


This program uses an HTML form to ask for the users name.

 <form method="post" action="/scripts/helloworld.php">
            <label for="firstname">What's your name?:</label>
            <input type="text" id="firstname" name="firstname" /><br />
                <input type="submit" value="Submit" name="submit" />
        </form>

The PHP part of this program saves the data from the form to a variable and then says hello to whatever is inside the variable. Which could be the users name or whatever they decided to input into the form. I didn’t bother to validate anything.

<?php
	$first_name = $_POST['firstname'];
if ($first_name) {
        echo "<h2>Hello $first_name!</h2>";
}
if ($first_name) {
        echo "<p class=\"textbox\">I know, there really isn't much wow factor to this either.<br />";
        echo "But this at least shows PHP's dynamic capabilities.<br />";
        echo "You can't replicate these results with just html by itself</p>";
}
?>

Posted in Code, PHP

Hello World

<?php
echo "Hello World!";
?>

I figured I would kick this off with an easy one. I’d link to a working example, but with PHP you really can’t tell the difference between an Hello World echo and an HTML Hello World. I know lame right?

Posted in Code, PHP