473,461 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ


PHP Date and Extras

By Blair Ireland
Senior Editor, TheScripts.com

Date

The date function is written in the following format; date(string format, int timestamp); The value returned will be in string format. Characters recognized in the formatting of the date function are as follows;

a - "am" or "pm"
A - "AM" or "PM"
d - day of the month, 2 digits with leading zeros; i.e. "01" to "31"
D - day of the week, textual, 3 letters; i.e. "Fri"
F - month, textual, long; i.e. "January"
h - hour, 12-hour format; i.e. "01" to "12"
H - hour, 24-hour format; i.e. "00" to "23"
g - hour, 12-hour format without leading zeros; i.e. "1" to "12"
G - hour, 24-hour format without leading zeros; i.e. "0" to "23"
i - minutes; i.e. "00" to "59"
j - day of the month without leading zeros; i.e. "1" to "31"
l (lowercase 'L') - day of the week, textual, long; i.e. "Friday"
L - boolean for whether it is a leap year; i.e. "0" or "1"
m - month; i.e. "01" to "12"
n - month without leading zeros; i.e. "1" to "12"
M - month, textual, 3 letters; i.e. "Jan"
s - seconds; i.e. "00" to "59"
S - English ordinal suffix, textual, 2 characters; i.e. "th", "nd"
t - number of days in the given month; i.e. "28" to "31"
U - seconds since the epoch
w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)
Y - year, 4 digits; i.e. "1999"
y - year, 2 digits; i.e. "99"
z - day of the year; i.e. "0" to "365"
Z - timezone offset in seconds (i.e. "-43200" to "43200")

If a character not recognized is present in the formatting string, then it will be output as it is.

There are so many different uses for PHP, which is why it is so popular now.

Lets say you want to check what sort of browser the person viewing the page is using. To do this, we must check the user agent string the browser is sending us to get the document. Similar to Perl as well, variables are always started with a $ sign in front of the variable name. The information about the type of browser the user has is stored in the variable $HTTP_USER_AGENT. To display this information, like you saw in the exaple above, you would add something like the following to your PHP script; <?php echo $HTTP_USER_AGENT; ?>

In order to do that we check the user agent string that the browser sends as part of its request. This information is stored in a variable. Variables always start with a dollar-sign in PHP. The variable we are interested in is $HTTP_USER_AGENT. To display this variable we can simply do: <?php echo $HTTP_USER_AGENT; ?>

You can then customize your messages according to this as well. I will introduce a few functions before you view the below; strstr() - this is a function that has been built into PHP to search a string for another string. if - this function is found in all programming languages. It follows the C++, Perl, and many other languages format. The IF statement will most likely be your most used function in PHP.

I will show you in the below example how to jump in and out of PHP commands and just output straight HTML, making it easier to understand. The flow of the script will not change at all, and only one HTML block will be outputted.

<?
php if(strstr($HTTP_USER_AGENT, "MSIE")) {
?>
<center><b>You are using Internet Explorer</b></center>
<?
} elseif (strstr($HTTP_USER_AGENT, "Mozilla/4.5")) {
?>
<center><b>You are using Netscape Navigator 4.5</b></center>
<?
} else {
?>
<center><b>You are not using Internet Explorer or Netscape
Navigator 4.5</b></center>
<?
}
?>

« PHP Introduction  

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.