sub datetime
{ 
 my ($format) = shift; 					# We take one argument, which says if we want the date, time, or both.
 my ($date,$time,$meridian,$returned);	# Declare our variables as local to this subroutine.

 # Get the GMT date and time in list context (ie. second, minute, hour, etc):
 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime();

 # If the time difference set is greater than 13 hours either way, ignore the difference.
 # This is because there are no such time zones that are more than 13 hours apart from GMT.

 if ($timedif > 13 || $timedif < -13){$timedif = 0;}
	
	# If the time difference is valid, do the following:

	# 1. Add the time difference in hours ($timedif) to the current hour (stored in $hour).
	# 2. If $hour is now greater than 12, take 12 hours from it and assume it's PM.
	
	# 3. If $hour is still greater than 12 after this, do the following:
	#		a. Aassume it's AM.
	#		b. Subtract 12 hours.
	#		c. Increment the day ($mday) by one.
	
	# 4. If the hour is equal to 12, assume it's PM.
	# 5. If it's less than 12, we can assume it's AM.
	
	# 6. If the hour is less than 1, do the following:
	#		a. Add 12 to the hour.
	#		b. Subtract one from the day.
	#		c. Assume it's now PM.
	#		d. If the hour is still less than one, add another 12 hours.

 $hour += $timedif;

 if ($hour > 12){
  $hour -= 12;
  $meridian = "PM";
 
  if ($hour > 12){
   $hour -= 12;
   $meridian = "AM";
   $mday++;
  }
 }
 elsif($hour == 12){$meridian = "PM";}
 else{$meridian = "AM";}

 if ($hour < 1){
  $hour += 12;
  $mday--;
  $meridian = "PM";
  if ($hour < 1){$hour += 12;}
 }

	# Now that we have done all our calculations, we just need some formatting:
	
	# 1. Format the date as: dd/mm/yyyy (e.g 09/01/2003).
	# 2. Format the time as: hh:mm AM/PM (e.g 07:51 PM).
	
 $date = sprintf("%0.2d/%0.2d/%0.4d", $mday,$mon+1,$year+1900);
 $time = sprintf("%0.2d:%0.2d $meridian",$hour,$min);


	# The final thing now is to work out what was requested (ie. date, time, or both).
	# This is done by checking the value of the arguement $format.
	
	# 1. If the argument was "date", return the date.
	# 2. If the argument was "time", return the time.
	# 3. If no argument was supplied, return both date and time.
	
 if ($format eq "date"){
  $returned = $date;
 }elsif ($format eq "time"){
  $returned = $time;
 }else{
  $returned = "$date $time";
 }
 
 	# Below is a quick and easy handler to get dynamic dates and times.
 	# This would be used in place of SSI where u wanted your dates to match the script.
 	# Usage: http://www.yoursite.com/cgi-bin/guestwho.cgi?action=showtime
 	
 if ($action eq "showtime"){
  print "Content-type: text/html\n\n";
  print "$returned";
  exit;
 }else{
  return $returned;
 }
}	
1;