Forum Moderators: coopster & phranque

Message Too Old, No Replies

Pattern Matching

converting dates - mm/dd/yy to text.....

         

polygonwindow

1:05 pm on Oct 29, 2002 (gmt 0)

10+ Year Member



Hi All... Im new here and new to Perl/CGI... im finding it quite interesting...

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....

andreasfriedrich

3:21 pm on Oct 29, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just some initial setup.

$_ = '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]

ukgimp

3:33 pm on Oct 29, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld

//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

polygonwindow

3:47 pm on Oct 29, 2002 (gmt 0)

10+ Year Member



hey thanks so much guys.. iam going to give those a bash and ill let u know how it goes..

polygonwindow

3:51 pm on Oct 29, 2002 (gmt 0)

10+ Year Member



hi andreasfriedrich...were u going to re-submit something else?

andreasfriedrich

3:57 pm on Oct 29, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi andreasfriedrich...were u going to re-submit something else?

Yes, have a look at it now.

polygonwindow

4:06 pm on Oct 29, 2002 (gmt 0)

10+ Year Member



thanks... by the way will this work if im using a form(html).. for example if i type text and then a date format..it should execute this script?

andreasfriedrich

4:12 pm on Oct 29, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are using Lincoln Stein´s CGI module [search.cpan.org] then you would need to get the submitted date like this:

$date = $q->param('date');

And then run $date through the code I posted.

Andreas

polygonwindow

4:26 pm on Oct 29, 2002 (gmt 0)

10+ Year Member



Andreas.. sorry to keep bothering u with this.. i just tested out the script i didnt work due to it saying i had a Internal Server Error.... i guessing i might have something wrong in the code...
this is what i have done from you...: (by the way im running a PC with Apache)

#!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....

andreasfriedrich

4:45 pm on Oct 29, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is that your entire script or are those just excerpts?

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

polygonwindow

4:51 pm on Oct 29, 2002 (gmt 0)

10+ Year Member



ok basically this is the whole script:

#!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....

ukgimp

4:55 pm on Oct 29, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you changed the pipes

"¦" should be solid instead.

Just a thought

polygonwindow

5:00 pm on Oct 29, 2002 (gmt 0)

10+ Year Member



thanks ukgimp.. tried it..but no difference... i reckon theres something missing in the code that i might of left or one of those really strange things.

andreasfriedrich

5:12 pm on Oct 29, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ukgimp got that right, you need to replace the brocken pipe character with the solid one.

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;

polygonwindow

5:23 pm on Oct 29, 2002 (gmt 0)

10+ Year Member



U guys are the best!
Thank - you so much it works like a charm!
Andreas u have been a big help for me today..that goes for u too ukgimp...yep that dodgy pipe hey!

once again thank - you!

[edited by: jatar_k at 5:56 pm (utc) on Oct. 29, 2002]