in reply to Re^4: Comparing Dates
in thread Comparing Dates

I tested my code a bit before posting it. For example,
'03/10/2006 4:39:11 PM' compared to '03/13/2006 4:39:11 PM' was false, while
'03/10/2006 4:39:11 PM' compared to '03/13/2006 4:39:12 PM' was true.

Can you give me the exact date strings that are resulting in problems? It sounds like the regexp isn't matching (so all the variables are undefined and treated as 0s).

Below is my code with a a die statement added to confirm the above:

sub get_time_from_local_time_string { my ($time_string) = @_; my ($M, $D, $Y, $h, $m, $s, $ampm) = $time_string =~ m{(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+) (\w+)} or die("Bad input"); if ($h == 12) { $h -= 12 if lc($ampm) eq 'am'; } else { $h += 12 if lc($ampm) eq 'pm'; } return timelocal($s, $m, $h, $D, $M-1, $Y); }

Replies are listed 'Best First'.
Re^6: Comparing Dates
by perl_99_monk (Novice) on Mar 14, 2006 at 15:23 UTC
    Yes it is bad data.... I am getting the dates from the database fields and using them. The strings are in this format: 03/22/2006 12:00:00 PM. Thanks

      So did you check your data?

      print get_time_from_local_time_string('03/22/2006 12:00:00 PM');

      prints

      1143046800

      Something you've told us isn't true. Are you sure you are passing to the sub what you think you are passing to the sub? Print it before passing it to the sub.

        Thanks so much... Finally got it to working after slight modifications...
        Whn i print them out I have the values like this... time1: 2006-03-29 12:00:00 time2: 2006-03-14 10:03:38 I am extremely sorry for the confusion but i am pulling the data from the db in to variables and then trying to do a diff. The way it displays on the form is 03/14/2006 10:05:50 AM so I got confused on the way it is stored... Thanks