Results 1 to 6 of 6

Thread: Perl Date Formating

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517

    Perl Date Formating

    Im kinda lost, I need all of the dates to be formated to a Postgres happy state. Can you help?


    Code I have been trying to work with (its lying.. not php):
    PHP Code:
            if($format eq "MM-DD-YYYY")
            {
                if(
    $data =~ /-/){
                    (
    $month,$day,$year) = split("-",$data) if $data =~ /-/;
                    (
    $month,$day,$year) = split("/",$data) if $data =~ /\//;
                    
    $month "01" if ($month == 0);
                    
    $year "2000" if ($year == 0);
                    
    $day "01" if ($day == 0);
                    
    $data="$year-$month-$day";
                } else {
                    
    #try to form a correct date
                    
    if(length($date) == || length($date) == 6) {
                        
    #2004-01-01 (20040101)
                        
    $date =~ s/\d{2}\d{2}\d{4}/\d{2}-\d{2}-\d{4}/e;
                    
                    }

                } 
    Thanks!!
    Last edited by Evan; Jan 30th, 2004 at 07:16 PM.

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    ps postgres likes 2003-01-01

  3. #3
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218

    Re: Perl Date Formating

    Originally posted by Evan
    PHP Code:
            if($format eq "MM-DD-YYYY")
            {
                if(
    $data =~ /-/){
                    (
    $month,$day,$year) = split("-",$data) if $data =~ /-/;
                    (
    $month,$day,$year) = split("/",$data) if $data =~ /\//;
                    
    $month "01" if ($month == 0);
                    
    $year "2000" if ($year == 0);
                    
    $day "01" if ($day == 0);
                    
    $data$year"-" $month "-" $day;
                } else {
                    
    #try to form a correct date
                    
    if(length($date) == || length($date) == 6) {
                        
    #2004-01-01 (20040101)
                        
    $date =~ s/(\d{2})(\d{2})(\d{4})/$3-$2-$1/e;
                    
                    }

                } 
    Mind you, I haven't tested that out and I don't really know what you are trying to do, so double check in the documentation (links in sig).

    I'm trying to use backticks. If you put parenthesises around an expression in a regex, you can call on that expression in the replace. I think $n will do that.

    Also, the period is the string concat operator in Perl.
    Travis, Kung Foo Journeyman

    Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
    Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    OSS: Mozilla, MySQL (Manual)

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Also, the period is the string concat operator in Perl.
    Whats a string concat operator?
    THanks!


    (Like String = "You have " & gold & " peices for sale" ) ??

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Oh I see now. It is like that.

    Very very cool. Learned something new!

    THanks alot!

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    ended up doing this

    PHP Code:
                        $month substr($data02);
                        
    $day =  substr($data22);
                        
    $year =  substr($data42) if( length($data) == 6);
                        
    $year =  substr($data44) if( length($data) == 8);
                        
    $data="$year-$month-$day"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width