Member Avatar for evilbenking

you guys ready for this, this is the worlds bigest problem, i will be super happy if you can help me

here is some code i have for a countdown script

"
#!/usr/bin/perl


##############################################################################
# Define Variables


# @from_date = (yyyy,mm,dd,hh,mm,ss);
# Which means: (year,month,day,hour,minute,second)
@from_date = (2006,2,15,00,00,XX);


# Done
##############################################################################


$ENV{'QUERY_STRING'} =~ s/%2C/,/g;
$ENV{'QUERY_STRING'} =~ s/=//g;


if ($ENV{'QUERY_STRING'}) {
@from_date = split(/,/, $ENV{'QUERY_STRING'});
}


# Define when various things occur, different dates, etc...
&define_dates;


# Calculate the Differences in the two dates
&calc_dates;


# Make Sure we don't get negative times.. That's not cool...
&no_negative;


# Top of HTML Page Information
&html_header;


# We don't want it to say 1 Years, now, do we?  Of course not!
&proper_english;


# End of HTML Page Information
&html_trailer;


#####################################
# Subroutines


sub define_dates {
($f_year,$f_month,$f_day,$f_hour,$f_minute,$f_second) = @from_date;


($second,$minute,$hour,$day,$month,$year,$wday,$yday,$isdst) = localtime(time());


$year += 1900;


&leap_year_check;


@months = ("XX","January","February","March","April","May","June","July",
"August","September","October","November","December");


@days = ("XX","1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th",
"11th","12th","13th","14th","15th","16th","17th","18th","19th",
"20th","21st","22nd","23rd","24th","25th","26th","27th","28th",
"29th","30th","31st");


@days_in_month = (31,$feb_days,31,30,31,30,31,31,30,31,30,31);


$date_term = "$months[$f_month] $days[$f_day]";


unless ($f_year eq 'XX') {
$date_term = "$date_term, $f_year";
}
unless ($f_hour eq 'XX') {
$date_term = "$date_term $f_hour";
}
unless ($f_minute eq 'XX') {
if ($f_minute < 10) {
$date_term = "$date_term:0$f_minute";
}
else {
$date_term = "$date_term:$f_minute";
}
}
unless ($f_second eq 'XX') {
if ($f_second < 10) {
$date_term = "$date_term:0$f_second";
}
else {
$date_term = "$date_term:$f_second";
}
}


$current_date = "$months[($month + 1)] $days[$day], $year $hour";
if ($minute < 10) {
$current_date = "$current_date:0$minute";
}
else {
$current_date = "$current_date:$minute";
}
if ($second < 10) {
$current_date = "$current_date:0$second";
}
else {
$current_date = "$current_date:$second";
}


}


sub leap_year_check {
if ($year % 4 != 0 || ($year % 100 == 0 && $year % 400 != 0)) {
$feb_days = "28";
}
else {
$feb_days = "29";
}
}


sub calc_dates {
$real_year = ($f_year - $year);
$real_month = (($f_month - 1) - $month);
$real_day = ($f_day - $day);
$real_hour = ($f_hour - $hour);
$real_minute = ($f_minute - $minute);
$real_second = ($f_second - $second);
}


sub no_negative {
if ($real_second < 0) {
$real_second = ($real_second + 60);
$real_minute--;
}


if ($real_minute < 0) {
$real_minute = ($real_minute + 60);
$real_hour--;
}


if ($real_hour < 0) {
$real_hour = ($real_hour + 24);
$real_day--;
}


if ($real_day < 0) {
$real_day = ($real_day + @days_in_month[$month]);
$real_month--;
}


if ($real_month < 0) {
$real_month = ($real_month + 12);
$real_year--;
}
}


sub proper_english {
unless ($f_year eq 'XX') {
if ($real_year eq '1') {
print "$real_year Year<br>\n";
} else {
print "$real_year Years<br>\n";
}
}


unless ($f_month eq 'XX') {
if ($real_month eq '1') {
print "$real_month Month<br>\n";
} else {
print "$real_month Months<br>\n";
}
}


unless ($f_day eq 'XX') {
if ($real_day eq '1') {
print "$real_day Day<br>\n";
} else {
print "$real_day Days<br>\n";
}
}


unless ($f_hour eq 'XX') {
if ($real_hour eq '1') {
print "$real_hour Hour<br>\n";
} else {
print "$real_hour Hours<br>\n";
}
}


unless ($f_minute eq 'XX') {
if ($real_minute eq '1') {
print "$real_minute Minute<br>\n";
} else {
print "$real_minute Minutes<br>\n";
}
}


unless ($f_second eq 'XX') {
if ($real_second eq '1') {
print "$real_second Second<br>\n";
} else {
print "$real_second Seconds<br>\n";
}
}



}


sub html_header {
print "Content-type: text/html\n\n";
print "<html><head><title>Countdown to : $date_term</title></head>\n";
print "<body><center><h1>Countdown till the day we legaly own our empire on : $date_term</h1>\n";
print "<hr>\n";
}


sub html_trailer {
print "<hr>\n";
print "It is currently $current_date\n";
print "</center>\n";
print "</body></html>\n";
}
"
but this uses the servers time, my server time, this is set to Australian WST and i am in england london, can some one edit this script so it uses client side time. this one is a pain in the ass for me

Recommended Answers

All 9 Replies

The world's biggest problem? I hope that's not true.

Next time you post, please put your code in code tags. Furthermore, in order to answer your question, code isn't really needed. Now, to answer your question...

There isn't a way to get the client time unless the client tells you what the time is on their system. Since there isn't a way to force the client to send the current time on the machine with it's request header, you'll have to find a different way to get that information. I recommend using Javascript.

There are two different ways to use Javascript to get the current time on the page: have it pass the time as part of the URL string or have Javascript print the time on the page itself. Each method has it's strengths and weeknesses. Passing the time as a URL encoded variable allows you to do fancy stuff with the time in Perl before printing the page but the time on the page will reflect that time that the page with the link was loaded, not when the current page was generated. Having Javascript on the page allows the time to be current and could automatically update the time dynamically but results in there being no purpose to the page being generated by Perl (in other words, a standard HTML page could do the exact same thing).

The following is a complete HTML page that will show the current time (see it here)

<html>
<head><title>Current time</title></head>
 <body>
 <script language="JavaScript">
 <!--
  time = new Date();
  minutes = time.getMinutes();
  if(minutes < 10){
        minutes = "0" + minutes;
  }

 seconds = time.getSeconds();
 if(seconds < 10){
        seconds = "0" + seconds;
 }
 document.write("Current time: ", time.getHours(), ":", minutes, ":", seconds);
 -->
</script>
</body>
</html>

Notice that this did not require any Perl code.

The following HTML page uses Javascript to create a link that will link to a program that will use the passed in time as part of the page generation (see it here:

<html>
<head><title>Current time</title></head>
<body>
<script language="JavaScript">
<!--
 time = new Date();
 minutes = time.getMinutes();
 if(minutes < 10){
    minutes = "0" + minutes;
 }
 seconds = time.getSeconds();
 if(seconds < 10){
    seconds = "0" + seconds;
 }

 document.write('<a href="/cgi-bin/showClientTime.cgi?time=', time.getHours(), ":", minutes, ":", seconds, '">Show Time</a>');
-->
</script>
</body>
</html>

The code I used in showClientTime.cgi is really simple and simply returns the time in a plain text format.

#!/usr/bin/perl
use strict;
use CGI;
my %FORM;
my $query = new CGI;
foreach my $key (sort {$a <=> $b} $query->param())
{
        $FORM{$key} = $query->param($key);
}
print "content-type: text/plain\n\n";

print $FORM{'time'};

exit;

I would recommend just using Javascript for your problem. The times when you want to use passed Javascript URL-encoded variables is when you are generating an image. Using passed variables and image generating code, you can have images that show the current client time on the page.

Does that help you at all?

Member Avatar for evilbenking

that does help for some things greatly, however the scipt is a countdown from a form take a look ...
here ...
i need to make it use the time of the person browsing the page if i can do that by putting a java script of the html page before the pl script then that would be great!

I think you still don't fully understand the limitation here. You cannot use the client time in a Perl script unless you already have the client time passed in. This means that you cannot load a page and magically get the client time from no where. If you want to do manipulations with the client time, use Javascript. I produced an end of year script using nothing but Javascript (example).

Member Avatar for evilbenking

I think you still don't fully understand the limitation here. You cannot use the client time in a Perl script unless you already have the client time passed in.

the link was to a html page, is there a javascript i can use on that page to pass the time then get the time that is passed on from that page in the perl script? if that is possiable do you think you could tell me the java and help remodle the get time part of the script

Why is it so important that you do this with Perl? Javascript was made for the very reason of giving web programmers access to information on the client-side of the web. Making really roundabout ways of getting the client time to the Perl code without requiring page reloads or page ugliness is doing nothing more than making what you want to do hard on yourself.

I've given you examples of how to pass the client-side information to a Perl script using Javascript, and I've shown you how to use Javascript to do the entire process. What more would you like me to show you?

Member Avatar for evilbenking

right the script's used on here are the sort i think i should use but how can i use the time from the java you have given on the example in my code to form my countdown?

I'm still a bit confused about why you want to do this with Perl. Are you wanting to learn how to do date and time manipulation with Perl? Is it a personal challenge? Did you tell someone that you could, so now you are trying to prove it?

Even if you made a page that used Javascript to link to a Perl script so that you got the client time, you still couldn't dynamically update it as I had shown in the last page I made, you need to use Javascript to do that.

Perl is useful for doing server-side stuff, not client-side. Since you want to manipulate client-side data, there isn't a reason to not use Javascript for the task.

While knowledge of how to do everything with a specific language is a good skill to have, knowing what language to use to solve a certain problem is much more valuable.

So... Why do you want to use Perl for this task? What do you expect Perl to do that Javascript can't? If you let me know what it is that causes you to want to use Perl, I can give you the information you need, otherwise I'm just throwing code at you.

Member Avatar for evilbenking

i got the script when my site was young, it does what i want mostly there is a form on the page for people to make there own count downs, and then there is a link to get the countdown to 2006 and my birthday. when you want my birthday its set as defalt (www.domail.net/cgi-bin/countdown.pl to view click here
or if you want 2006 it does www.domain.net/countdown.pl?yyyy,mm,dd,hh,mm,ss (year,month,day,hour,mins,seconds) the script just takes the current date then works out the difference bettwen the two (i believe) then displays a count down (that does not refresh or change it's just form when you went to the page all i would like to do is somehow pass the client time to it so that it does not use my australien server time (sorry about any mis-spellings here)

Keep in mind that Javascript is good for client-side things and Perl is good for server-side things. Knowing this, the best design would be to use Perl to create Javascript code based upon form selections. I made a sample script to show this in action (example):

#!/usr/bin/perl

use strict;

use CGI;

my $scriptName = "/cgi-bin/countdownTimer.cgi";

my %FORM;
my $query = new CGI;
foreach my $key (sort {$a <=> $b} $query->param())
{
	$FORM{$key} = $query->param($key);
}




sub selectCountdownOptions
{
	if($FORM{'optionsSelected'} eq '')
	{
		my ($second, $minute, $hour, $day, $month, $year) = localtime(time);
		
		$year += 1900;
		$month += 1;
		
		$FORM{'year'} = $year;
		$FORM{'month'} = $month;
		$FORM{'day'} = $day;
		$FORM{'hour'} = 0;
		$FORM{'minute'} = 0;
		$FORM{'second'} = 0;
	}
	
	print "content-type: text/html;\n\n";
	
	print qq|<html>
<head><title>Custom Countdown</title></head>

<body>

<font color="red">$FORM{'errorMessage'}</font>

<form method="POST" enctype="multipart/form-data" action="$scriptName">

<table>
	<tr><td colspan="2">Please select the date and time you would like to count to:</td></tr>
	<tr><td>Date Description</td><td><input type="text" size="20" maxlength="50" name="description" value="$FORM{'description'}"></td></tr>
	<tr><td>Year</td><td><input type="text" size="4" maxlength="4" name="year" value="$FORM{'year'}"></td></tr>
	<tr><td>Month</td><td><input type="text" size="2" maxlength="2" name="month" value="$FORM{'month'}"></td></tr>
	<tr><td>Day</td><td><input type="text" size="2" maxlength="2" name="day" value="$FORM{'day'}"></td></tr>
	<tr><td>Hour</td><td><input type="text" size="2" maxlength="2" name="hour" value="$FORM{'hour'}"></td></tr>
	<tr><td>Minute</td><td><input type="text" size="2" maxlength="2" name="minute" value="$FORM{'minute'}"></td></tr>
	<tr><td>Second</td><td><input type="text" size="2" maxlength="2" name="second" value="$FORM{'second'}"></td></tr>
	<tr><td colspan="2" align="center"><input type="submit" name="showCountdown" value="Show Countdown"></td></tr>
</table>

<input type="hidden" name="optionsSelected" value="1">

</form>

</body>

</html>|;
	
	exit;
}

sub hasErrors
{
	return 1 if($FORM{'errorMessage'} ne '');
	return '';
}

sub setErrorMessage
{
	if($FORM{'errorMessage'} ne '')
	{
		$FORM{'errorMessage'} .= "<br>";
	}
	
	$FORM{'errorMessage'} .= shift;
}

sub showCountdown
{
	setErrorMessage("Invalid month") if(($FORM{'month'} < 1) || ($FORM{'month'} > 12));
	setErrorMessage("Invalid day") if(($FORM{'day'} < 1) || ($FORM{'day'} > 31));
	setErrorMessage("Invalid hour") if(($FORM{'hour'} < 0) || ($FORM{'hour'} > 24));
	setErrorMessage("Invalid minute") if(($FORM{'minute'} < 0) || ($FORM{'minute'} > 59));
	setErrorMessage("Invalid second") if(($FORM{'second'} < 0) || ($FORM{'secoind'} > 59));
	
	$FORM{'hour'} = 0 if($FORM{'hour'} == 24);
	
	print "content-type: text/html\n\n";
	
	print qq|<html>
<head><title>$FORM{'description'}</title></head>

<body>
<script language="JavaScript">
<!--

function showTime()
{
	var today = new Date();
	var target = new Date($FORM{'year'}, | . ($FORM{'month'} - 1) . qq|, $FORM{'day'}, $FORM{'hour'}, $FORM{'minute'}, $FORM{'second'});
	
	var numMilliseconds = Math.abs(target.getTime() - today.getTime());
	
	var numDays = Math.floor(numMilliseconds / (1000 * 60 * 60 * 24));
	numMilliseconds -= numDays * 1000 * 60 * 60 * 24;
	if(numDays == 1)
	{
		numDays += " day ";
	}
	else
	{
		numDays += " days ";
	}
	
	var numHours = Math.floor(numMilliseconds / (1000 * 60 * 60));
	numMilliseconds -= numHours * 1000 * 60 * 60;
	if(numHours == 1)
	{
		numHours += " hour ";
	}
	else
	{
		numHours += " hours ";
	}
	
	var numMinutes = Math.floor(numMilliseconds / (1000 * 60));
	numMilliseconds -= numMinutes * 1000 * 60;
	if(numMinutes == 1)
	{
		numMinutes += " minute ";
	}
	else
	{
		numMinutes += " minutes ";
	}
	
	var numSeconds = Math.floor(numMilliseconds / 1000);
	if(numSeconds == 1)
	{
		numSeconds += " second ";
	}
	else
	{
		numSeconds += " seconds ";
	}
	
	if((target.getTime() - today.getTime()) < 0)
	{
		document.getElementById("countdown").innerHTML = "$FORM{'description'} was " + numDays + numHours + numMinutes + numSeconds + " ago!";
	}
	else
	{
		document.getElementById("countdown").innerHTML = "$FORM{'description'} is just " + numDays + numHours + numMinutes + numSeconds + " away!";
	}
	
	setTimeout('showTime()', 1000);
}

-->
</script>

<div id="countdown"></div>

<script>
<!--

showTime();

-->
</script>

</body>

</html>|;
	
	exit;
}


showCountdown if($FORM{'showCountdown'} ne '');

selectCountdownOptions;

Is this more of what you are looking for?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.