Print Current Month


/ Published in: Perl
Save to your folder(s)

This is a script that simply prints the current month. I produced this a couple years ago and use it as a reference for using the localtime() function.


Copy this code and paste it in your HTML
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
  7.  
  8. # You add 1 because the number in $month is based on a starting number of 0.
  9. # So, if $month is 0, then $monthnum is 1 (ie: January)
  10. # Adding 1 gets you a number from the list below.
  11.  
  12. my $monthnum = $month + 1;
  13.  
  14. my %monthname = (
  15. 1 => 'January',
  16. 2 => 'February',
  17. 3 => 'March',
  18. 4 => 'April',
  19. 5 => 'May',
  20. 6 => 'June',
  21. 7 => 'July',
  22. 8 => 'August',
  23. 9 => 'September',
  24. 10 => 'October',
  25. 11 => 'November',
  26. 12 => 'December',
  27. );
  28.  
  29. print("The present month is: $monthname{$monthnum}\n");

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.