Solved

problem with date sub

Posted on 1998-06-22
7
169 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

910 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

21 Experts available now in Live!

Get 1:1 Help Now