Reply to topic
Non working perl countdown script
Jonhoo


Joined: 05 Nov 2005
Posts: 1
Reply with quote
Hi Smile I've just written a countdown script in perl which is supposed to print how many days, hours, minutes and seconds there are left until a certain date (Specified in the first variables). The code is as following:

Code:

$yr = 2006;
$mo = 4;
$da = 13;
$hr = 0;
$min = 00;
$sec = 00;
$occasion="TG06!";
$message_on_occasion="The Gathering 06 er her!";

sub count()
{

@today=localtime(time());

$todayy=@today [5];

   if ($todayy < 1000)
   {
      $todayy+=1900;
   }

$todaym=@today [4];

$todayd=@today [3];

$todayh=@today [2];

$todaymin=@today [1];

$todaysec=@today [0];

$dd=($sec + ($min*60) + ($hr*3600) + ($da*3600*24) + ($mo*365/12*24*3600) + ($yr*365*24*3600)) - ($todaysec + ($todaymin*60) + ($todayhr*3600) + ($todayda*3600*24) + ($todaymo*365/12*24*3600) + ($todayyr*365*24*3600));

$dday=int($dd/(60*60*1000*24)*1);

$dhour=int(($dd%(60*60*1000*24))/(60*60*1000)*1);

$dmin=int((($dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);

$dsec=int((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);

system "cls";

   if ($dday<=0&&$dhour<=0&&$dmin<=0&&$dsec<=1&&$todayd==$da)
      {print $message_on_occasion;}
   elsif ($dday<=-1)
      {print $occasion+" er i gang, eller kanskje til og med ferdig :( Da er det bare å vente til neste år :D";}
   else
      {print "Det er "+$dday+" dager, "+$dhour+" timer, "+$dmin+" minutter og "+$dsec+" sekunder igjen til "+$occasion;}
      
sleep 1;
count();
}

count();
pmeserve
HostMySite Tech

Joined: 19 Mar 2004
Posts: 178
Reply with quote
Hmm, I'm no good with Perl. I actually have something almost identical in PHP - shows number of minutes/hours/days/weeks/months/years and does past and future. Won't show seconds but it would be easy to modify it for that if you wanted it to:

Code:
function getTimeDiff($diff) // take difference in seconds and return formatted in best time unit
{
   $diff = $diff / 60;
   
   return (abs($diff) > 60) // more than an hour
         ? (abs($diff/60) > 24) // more than a day
            ? (abs($diff/60/24) > 30) // more than a month
               ? (abs($diff/60/24) > 365) // more than a year
                  ? formatDueDate($diff/60/24/365, "year")
                  : formatDueDate($diff/60/24/30, "month")
               : formatDueDate($diff/60/24, "day")
            : formatDueDate($diff/60, "hour")
         : formatDueDate($diff, "min");
}
function formatDueDate($time, $unit) // format a due date string, taking into account negative/positive and unit plurality
{
   $nice_time = ($unit != "minute" && $unit != "hour") // only round if unit is greater than minute/hour
      ? round($time, 1)
      : floor($time);
   $nice_time = abs($nice_time);
   
   if($time > 0) // if it's in the future
      return (abs($time) > 1) // if we need a plural unit
         ? "in $nice_time {$unit}s"
         : "in $nice_time {$unit}";
   else
      return (abs($time) > 1) // if we need a plural unit
         ? "$nice_time {$unit}s ago"
         : "$nice_time {$unit} ago";
}
Non working perl countdown script
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic