problem with date sub

Posted on 1998-06-22
Last Modified: 2010-05-18
I use this subroutine (orig from someone elses program) to get the time and date nicely formatted.  However instead of 1:15 it writes 13:15.  Anyone see whats wrong?  Thanks.
sub date {
      ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime (time);


      if ($hour < 12) {
            $AMPM = "AM";

      if ($hour > 12) {
            $hour = $hour - 12;
            $AMPM = "PM";
      if ($hour == 12) {
            $AMPM = "PM";
      if ($hour == 0) {
            $hour = "12";
      $min = sprintf ("%2d", $min);
      $min =~tr/ /0/;
      $mon = sprintf ("%2d", $mon);
      $mon =~tr/ /0/;
      $mday = sprintf ("%2d", $mday);
      $mday =~tr/ /0/;
      $HyphenDate = ("$mon" . "-" . "$mday" . "-" . "$year");
      $RunonDate = ("$mon$mday$year");
      $Time = ("$hour" . ":" . "$min" . " " . "$AMPM");
Question by:idadan
  • 3
  • 2
  • 2

Expert Comment

ID: 1207970
Yup.  This thing will always give the wrong time.  The culprit is that

$hour++ ;

By the way, I'm pretty sure that you could combine the if statements as:

if ($hour < 12) {
   $AMPM = 'AM'
elsif ($hour >= 12) {
    $hour -= 12;
    $AMPM = "PM";
if ($hour == 0) {
    $hour += 12;


Author Comment

ID: 1207971
I didn't try replacing the if statements but when i took out that hour++ it gave the time as an hour off, 4:27 read as 3:27.

Author Comment

ID: 1207972
The only time it gets the time wrong is for 1:00 to 1:59 no others are wrong.
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails


Accepted Solution

b2pi earned 50 total points
ID: 1207973
Not on my machines (linux and NT), or according to the documentation (Note that the MONTH has to be adjusted for the right time, but not the hour.)


localtime EXPR

Converts a time as returned by the time function to a 9-element array with the time analyzed for the local time zone. Typically used as follows:

    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =

All array elements are numeric, and come straight out of a struct tm. In particular this means that $mon has the range 0..11 and $wday has the range 0..6 and
$year is year-1900, that is, $year is 123 in year 2023. If EXPR is omitted, uses the current time (``localtime(time)'').

In a scalar context, returns the ctime value:

    $now_string = localtime;  # e.g., "Thu Oct 13 04:54:34 1994"

Also see the Time::Local module, and the strftime function available via the POSIX module.


Author Comment

ID: 1207974
Its a unix machine running apache, I'll look into another sub, thanks for the help.
LVL 84

Expert Comment

ID: 1207975

LVL 84

Expert Comment

ID: 1207976
($min, $hour) = (localtime)[1,2];
$Time = sprintf("%d:%02d %s",($hour-1)%12+1,$min,$hour<12?"AM":"PM");

use POSIX;
$Time=strftime "%I:%S %p",localtime;


Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
json encode mysql results 4 182
hard perl script 16 146
Awk Question 2 116
Perl script to delete older files 6 48
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now