PHP vs Perl

  • Thread starter John Taylor-Johnston
  • Start date
J

John Taylor-Johnston

Comparison study. Can this be done in Perl? How many lines?

<?php
$display_start = "2003-09-01";
$display_end = "2003-09-31";

echo "testing<br>";

$stuff = "$display_start - $display_end<br>";

if((date("Y-m-d") >= $display_start) && (date("Y-m-d") <= $display_end)){
print($stuff);
}else{
echo "Date expired: ".date("Y-m-d");
}
?>
 
J

Jay Tilton

: Comparison study. Can this be done in Perl?

Yes.

: How many lines?

How many do you want?
 
C

Charlton Wilbur

JTJ> Comparison study. Can this be done in Perl? How many lines?
JTJ> <?php
JTJ> $display_start = "2003-09-01";
JTJ> $display_end = "2003-09-31";
JTJ> echo "testing<br>";
JTJ> $stuff = "$display_start - $display_end<br>";
JTJ> if((date("Y-m-d") >= $display_start)
JTJ> && (date("Y-m-d") <= $display_end))
JTJ> { print($stuff); }else{ echo "Date expired:
JTJ> ".date("Y-m-d"); } ?>

It's about as complex in Perl as it is in PHP, and you can even take
the same approach. Here's a hint: perldoc POSIX, look for strftime.

Charlton
 
J

Jay Tilton

In future, please don't top-post replies.


: > : How many lines?
: > How many do you want?
:
: Shortest :)

Be careful. Those are golfing words. :)

: Seriously. Can someone show me?

PHP has an advantage with that nice date() function. The strftime()
function in Perl's mountainous POSIX module has similar capabilities.

use POSIX 'strftime';
($display_start, $display_end) = ("2003-09-01", "2003-09-31");
print "testing<br>",
($n=strftime '%Y-%m-%d', localtime) ge $display_start
&& $n le $display_end
? "$display_start - $display_end<br>"
: "Date expired: $n";
 
P

Peter Cooper

John Taylor-Johnston said:
Comparison study. Can this be done in Perl? How many lines?

Perhaps this horrible code will give you a clue :)

use Time::Local;
my $a = timelocal (0,0,0, 1, 8, 103);
my $b = timelocal (59,59,23, 30, 8, 103);
if ((time > $a) && (time < $b)) { print "x"; }

I personally do not advise using the POSIX functions, as Perl is
cross-platform (read: works on non-POSIX platforms too). Convert times to
and from epoch time using Time::Local and localtime/gmtime, etc.. or use
Time::piece objects (more elegant, and more code).

Pete
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Comparison study. Can this be done in Perl? How many lines?

<?php
$display_start = "2003-09-01";
$display_end = "2003-09-31";

echo "testing<br>";

$stuff = "$display_start - $display_end<br>";

if((date("Y-m-d") >= $display_start) && (date("Y-m-d") <= $display_end)){
print($stuff);
}else{
echo "Date expired: ".date("Y-m-d");
}
?>

Almost exactly the same:

use Time::Format;

$display_start = "2003-09-01";
$display_end = "2003-09-31"; # Did you really mean 30?

print "testing<br>";

$stuff = "$display_start - $display_end<br>";

if(($time{"yyyy-mm-dd"} ge $display_start) && ($time{"yyyy-mm-dd"} le
$display_end)){
print($stuff);
}else{
print "Date expired: $time{'yyyy-mm-dd'}";
}



- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP1ojomPeouIeTNHoEQJG0ACgzFDEgt5E5GRmj8v5hR+mW8vDH34AoPEB
FD85q2Q0QrCTqtNXQZE0tTPn
=3Hkg
-----END PGP SIGNATURE-----
 
P

Peter Cooper

Eric J. Roode said:
Almost exactly the same:

use Time::Format;

Very cool, never knew about this module before (there's always new ones
hiding around the corner, I find!). I'll have to check this one out.

Pete
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Very cool, never knew about this module before (there's always new ones
hiding around the corner, I find!). I'll have to check this one out.

Well, I think it's cool -- but I'm biased; I wrote it. :)

Thanks for your kind words, Peter.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP1tvrGPeouIeTNHoEQIupwCg9wvIhFb8N+2y7QVEBIw4OrOdc7sAn2h9
df2wE++VyKBuGL6ZhsaAeotJ
=r65E
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,137
Messages
2,570,795
Members
47,342
Latest member
eixataze

Latest Threads

Top