I was hoping someone could help figure how i could use Pattern matching to convert such dates as:
04-04-99,
04/04/99
- to 4th April 99 (1999)
I understand the matching and substituting statements... but was hoping there was somemone who could help on the right guidelines.... or even the code..hehe! Thanks....
$_ = '02-03-99';
%months = ('01' => 'January', '02' => 'February');
%pf = ('01' => 'st', '02' => 'nd', '03' => 'rd');
Now we need to parse the date string. There are several ways to do this.
The first one is the most efficient but is only suitable for simple formats.
@a = unpack('a2aa2aa2', $_);
$month = $a[ 0 ];
$day = $a[ 2 ];
$year = $a[ 4 ]; Using split will work well for simple formats as well.
@a = split /\/¦-/, $_;
$month = $a[ 0 ];
$day = $a[ 1 ];
$year = $a[ 2 ];
Using regular expression lets you parse more complex date formats. It comes with a much larger overhead.
m!(\d{2})(?:/¦-)(\d{2})(?:/¦-)(\d{2})!
$month = $1;
$day = $2;
$year = $3; Now that we have the date parsed we need to build the new string:
$date = sprintf('%d%s %s %s',
............... $day,
............... exists($pf{$day})? $pf{$day} : 'th',
............... $months{$month},
............... $year
...............);
print $date;
#
$date = sprintf('%s %d%s %s',
............... $months{$month},
............... $day,
............... exists($pf{$day})? $pf{$day} : 'th',
............... $year
...............);
print $date; The first sprintf will format a string like 3rd February 99 while the second sprintf gives you February 3rd 99.
You need to remove the leading dots.
See also
Date::Manip [search.cpan.org]
[edited by: andreasfriedrich at 3:57 pm (utc) on Oct. 29, 2002]
//PHP MySQL date format (2000-09-15)
$string = $datevariable;
$stringArray = explode("-", $string);
$date = mktime(0,0,0,$stringArray[1],$stringArray[2],$stringArray[0]);
$convertedDate = date("M j", $date);
echo $convertedDate;
Converted Date is where you do the formatting
$date = $q->param('date');
And then run $date through the code I posted.
Andreas
#!perl
use strict;
use CGI ':standard';
print "Content-type: text/html\n\n";
$date = $q->param('date');
m!(\d{2})(?:/¦-)(\d{2})(?:/¦-)(\d{2})!
$month = $1;
$day = $2; $year = $3;
$date = sprintf('%d%s %s %s',$day, exists($pf{$day})? $pf{$day} : 'th', $months{$month}, $year );
print $date;
Iam basing this of from entering text in textarea field within a form whose name is date...
appreciated....
Are you running Linux or Windows?
The #!perl line is certainly incorrect. It needs to contain the path to your perl executable.
To use $date = $q->param('date'); you would need to instantiate a new CGI object like this $q = new CGI();
m!(\d{2})(?:/¦-)(\d{2})(?:/¦-)(\d{2})! would need to be $date =~ m!(\d{2})(?:/¦-)(\d{2})(?:/¦-)(\d{2})!
You need to have the months and pf hash in your script.
If you corrected those errors, try running your script from the command line like this perl -w script date=04-04-99.
Hope this helps.
Andreas
#!perl
use strict;
use CGI ':standard';
print "Content-type: text/html\n\n";
#$date = $q->param('date'); (not sure of this)
$date = param('date');
$_ = '02-03-99';
%months = ('01' => 'January', '02' => 'February');
%pf = ('01' => 'st', '02' => 'nd', '03' => 'rd');
$date =~ m!(\d{2})(?:/¦-)(\d{2})(?:/¦-)(\d{2})!
$months = $1;
$day = $2;
$year = $3;
$date = sprintf('%d%s %s %s',$day, exists($pf{$day})? $pf{$day} : 'th', $months{$months}, $year );
print $date;
i have used #!perl before without problems as iam running Windows98 on a PC.. im running these scripts from a webpage. not sure wot u mean by putting the months and pf in a hash?
OOPS SORRY I HAVE PUT THE HASH IN....
You don´t need the $_ = '02-03-99'; That was just for testing purposes.
$date = param('date'); should work as well.
If you are using use strict you need to declare all your variables with my.
Did you try running the script from the command line?
You still need to add the names for the other months to the months hash.
This one works. You need to replace the broken pipe character.
#!perl
use strict;
use CGI ':standard';
print "Content-type: text/html\n\n";
my %months = ('01' => 'January', '02' => 'February', '03' => 'March');
my %pf = ('01' => 'st', '02' => 'nd', '03' => 'rd');
my $date = param('date');
$date =~ m!(\d{2})(?:/¦-)(\d{2})(?:/¦-)(\d{2})!;
my $month = $1;
my $day = $2;
my $year = $3;
$date = sprintf('%d%s %s %s',$day, exists($pf{$day})? $pf{$day} : 'th', $months{$month}, $year );
print $date;