Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

problem with date sub

Posted on 1998-06-22
7
Medium Priority
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Accepted Solution

by:
b2pi earned 100 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

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…
Six Sigma Control Plans

688 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