graff and
liverpole have given you some useful pointers. I would like to draw your attention to what I think is a fault with your calculation of AM/PM. On a 24-hour clock the hours of 0 through 11 are AM and 12 through 23 are PM. Your code should perhaps read
$ampm = 'AM';
if ($hour >= 12)
{
$hour -= 12;
$ampm = 'PM';
}
Also, when you are constructing your $datetime string you could construct the whole thing in one go with the sprintf. Note that %02d will do the same thing here as %2.2d.
$datetime = sprintf
'%d:%02d:%02d %s, %s, %s %d, %d',
$hour, $min, $sec, $ampm,
$thisday, $thismon, $mday, $year + 1900;
This looks a little clearer to my eye, particularly the format string which makes it much easier to visualise what the $datetime string will look like.
I hope this is of use.
Cheers,
JohnGG
Update: It occurs to me that 00:23 on a 24-hour clock is 12:23AM, not 0:23AM and 12:23 on a 24-hour clock is 12:23PM, not 0:23PM so revised code should in fact be
$ampm = 'AM';
if ($hour >= 12)
{
$hour -= 12;
$ampm = 'PM';
}
$hour += 12 unless $hour;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.