Programs rarely work correctly the first time. Many things can go
wrong in your program that cause the PHP interpreter to generate an
error message. You have a choice about where those error messages go.
The messages can be sent along with other program output to the web
browser. They can also be included in the web server error log.
To make error messages display in the browser, set the
display_errors configuration directive to
On. To send errors to the web server error log, set
log_errors to On. You can set them both to On if you want error messages in both places.
PHP defines some constants you can use to set the value of
error_reporting
such that only errors of certain types get reported: E_ALL (for all
errors except strict notices), E_PARSE (parse errors), E_ERROR (fatal
errors), E_WARNING (warnings), E_NOTICE (notices), and E_STRICT (strict
notices).
While writing your PHP program, it is a good idea to use PHP-aware editors like
BBEdit or
Emacs.
One of the speical special features of these editors is syntax
highlighting. It changes the color of different parts of your program
based on what those parts are. For example, strings are pink, keywords
such as if and while are blue, comments are grey, and variables are
black.
Another feature is quote and bracket matching, which helps to make
sure that your quotes and brackets are balanced. When you type a
closing delimiter such as }, the editor highlights the opening { that
it matches.
There are following points which need to be verfied while debugging your program.
- Missing Semicolons - Every PHP statement ends with a
semicolon (;). PHP doesn't stop reading a statement until it reaches a
semicolon. If you leave out the semicolon at the end of a line, PHP
continues reading the statement on the following line.
- Not Enough Equal Signs - When you ask whether two values are equal in a comparison statement, you
need two equal signs (==). Using one equal sign is a common mistake.
- Misspelled Variable Names - If you misspelled a variable
then PHP understands it as a new variable. Remember: To PHP, $test is
not the same variable as $Test.
- Missing Dollar Signs - A missing dollar sign in a
variable name is really hard to see, but at least it usually results in
an error message so that you know where to look for the problem.
- Troubling Quotes - You can have too many, too few, or the wrong kind of quotes. So check for a balanced number of quotes.
- Missing Parentheses and curly brackets - They should always be in pairs.
- Array Index - All the arrays should start from zero instead of 1.
Moreoever, handle all the errors properly and direct all trace
messages into system log file so that if any problem happens then it
will be logged into system log file and you will be able to debug that
problem.
Dates are so much part of everyday life that it becomes easy to work
with them without thinking. PHP also provides powerful tools for date
arithmetic that make manipulating dates easy.
Getting the Time Stamp with time():
PHP's
time() function gives you all the information that you
need about the current date and time. It requires no arguments but
returns an integer.
The integer returned by time() represents the number of seconds
elapsed since midnight GMT on January 1, 1970. This moment is known as
the UNIX epoch, and the number of seconds that have elapsed since then
is referred to as a time stamp.
It will produce following result:
This is something difficult to understand. But PHP offers excellent
tools to convert a time stamp into a form that humans are comfortable
with.
Converting a Time Stamp with getdate():
The function
getdate() optionally accepts a time stamp and
returns an associative array containing information about the date. If
you omit the time stamp, it works with the current time stamp as
returned by time().
Following table lists the elements contained in the array returned by getdate().
Key | Description | Example |
seconds | Seconds past the minutes (0-59) | 20 |
minutes | Minutes past the hour (0 - 59) | 29 |
hours | Hours of the day (0 - 23) | 22 |
mday | Day of the month (1 - 31) | 11 |
wday | Day of the week (0 - 6) | 4 |
mon | Month of the year (1 - 12) | 7 |
year | Year (4 digits) | 1997 |
yday | Day of year ( 0 - 365 ) | 19 |
weekday | Day of the week | Thursday |
month | Month of the year | January |
0 | Timestamp | 948370048 |
Now you have complete control over date and time. You can format this date and time in whatever format you wan.
Example:
Try out following example
<?php
$date_array = getdate();
foreach ( $date_array as $key => $val )
{
print "$key = $val<br />";
}
$formated_date = "Today's date: ";
$formated_date .= $date_array[mday] . "/";
$formated_date .= $date_array[mon] . "/";
$formated_date .= $date_array[year];
print $formated_date;
?>
|
It will produce following result:
seconds = 27
minutes = 25
hours = 11
mday = 12
wday = 6
mon = 5
year = 2007
yday = 131
weekday = Saturday
month = May
0 = 1178994327
Today's date: 12/5/2007
|
Converting a Time Stamp with date():
The
date() function returns a formatted string representing a
date. You can exercise an enormous amount of control over the format
that date() returns with a string argument that you must pass to it.
The date() optionally accepts a time stamp if ommited then current
date and time will be used. Any other data you include in the format
string passed to date() will be included in the return value.
Following table lists the codes that a format string can contain:
Format | Description | Example |
a | 'am' or 'pm' lowercase | pm |
A | 'AM' or 'PM' uppercase | PM |
d | Day of month, a number with leading zeroes | 20 |
D | Day of week (three letters) | Thu |
F | Month name | January |
h | Hour (12-hour format - leading zeroes) | 12 |
H | Hour (24-hour format - leading zeroes) | 22 |
g | Hour (12-hour format - no leading zeroes) | 12 |
G | Hour (24-hour format - no leading zeroes) | 22 |
i | Minutes ( 0 - 59 ) | 23 |
j | Day of the month (no leading zeroes | 20 |
l (Lower 'L') | Day of the week | Thursday |
L | Leap year ('1' for yes, '0' for no) | 1 |
m | Month of year (number - leading zeroes) | 1 |
M | Month of year (three letters) | Jan |
r | The RFC 2822 formatted date | Thu, 21 Dec 2000 16:01:07 +0200 |
n | Month of year (number - no leading zeroes) | 2 |
s | Seconds of hour | 20 |
U | Time stamp | 948372444 |
y | Year (two digits) | 06 |
Y | Year (four digits) | 2006 |
z | Day of year (0 - 365) | 206 |
Z | Offset in seconds from GMT | +5 |
Example:
Try out following example
<?php
print date("m/d/y G.i:s<br>", time());
print "Today is ";
print date("j of F Y, \a\\t g.i a", time());
?>
|
It will produce following result:
01/20/00 13.27:55
Today is 20 of January 2000, at 1.27 pm
|
Hope you have good understanding on how to format date and time
according to your requirement. For your reference a complete list of
all the date and time functions is given in
PHP Date & Time Functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
No comments:
Post a Comment