Solved

problem with date sub

Posted on 1998-06-22
7
176 Views
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);

      $mon++;

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

      if ($hour > 12) {
            $hour = $hour - 12;
            $AMPM = "PM";
      }        
      if ($hour == 12) {
            $AMPM = "PM";
      }        
            
      if ($hour == 0) {
            $hour = "12";
      }
      $hour++;
      $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");
      
}
0
Comment
Question by:idadan
  • 3
  • 2
  • 2
7 Comments
 
LVL 5

Expert Comment

by:b2pi
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;
}

0
 

Author Comment

by:idadan
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.
0
 

Author Comment

by:idadan
ID: 1207972
The only time it gets the time wrong is for 1:00 to 1:59 no others are wrong.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 5

Accepted Solution

by:
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.)

SYNOPSIS

localtime EXPR

DESCRIPTION
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) =
                                                localtime(time);

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.

0
 

Author Comment

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

Expert Comment

by:ozo
ID: 1207975

0
 
LVL 84

Expert Comment

by:ozo
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;

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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 …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

816 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

11 Experts available now in Live!

Get 1:1 Help Now