Solved

problem with date sub

Posted on 1998-06-22
7
185 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

828 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