|
-
Jan 30th, 2004, 07:08 PM
#1
Thread Starter
Frenzied Member
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) == 8 || 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.
-
Jan 30th, 2004, 07:10 PM
#2
Thread Starter
Frenzied Member
ps postgres likes 2003-01-01
-
Feb 2nd, 2004, 08:51 AM
#3
Addicted Member
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) == 8 || 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)
-
Feb 2nd, 2004, 10:47 AM
#4
Thread Starter
Frenzied Member
Also, the period is the string concat operator in Perl.
Whats a string concat operator?
THanks!
(Like String = "You have " & gold & " peices for sale" ) ??
-
Feb 2nd, 2004, 10:48 AM
#5
Thread Starter
Frenzied Member
Oh I see now. It is like that.
Very very cool. Learned something new!
THanks alot!
-
Feb 2nd, 2004, 12:14 PM
#6
Thread Starter
Frenzied Member
ended up doing this
PHP Code:
$month = substr($data, 0, 2);
$day = substr($data, 2, 2);
$year = substr($data, 4, 2) if( length($data) == 6);
$year = substr($data, 4, 4) 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|