Red Antigua Logo
Yet another piece of web.
Search this site (by Google)
Tools    (top)
Check a site for broken links
(W3C)

Perl modules    (top)
Tree::Numbered::Tools
(CPAN)
Perl tutorials    (top)
Perl modules
HTML::Template
CGI::Application
Cookies with CGI::Application
Upload files with CGI::Application
Download files with CGI::Application
Redirect with CGI::Application
CPAN shell
Install DBD::mysql from the CPAN shell
Perl trim function
Validate an IP with Perl
Run suid Perl scripts under Apache
Perl taint mode
Perl date functions with Date::Calc

In Spanish
Curso de Perl

C tutorials    (top)
C - Introduction
C - Absolute beginner's Emacs
C - Examples for beginners
C - Makefile examples
C - Autotools examples
Server configurations    (top)
DNS
Apache
Apache Authentication and Access Control
mod_perl on FreeBSD
MySQL
MySQL add account
phpMyAdmin
Squid
DHCP

UNIX on Windows    (top)
MSYS2 - UNIX environment for MS Windows 32/64 bits
Apache setup on Windows
MySQL setup on Windows
PHP setup on Windows
Perl setup on Windows
Emacs setup on Windows
PuTTY
WinSCP
GIMP on Windows
MinGW - gcc on Windows
MSYS - UNIX-styled shell on Windows
msysDTK - autotools on Windows
GDB for MinGW on Windows

Misc. FreeBSD/UNIX    (top)
'portupgrade' on FreeBSD
'ipf' on FreeBSD
'pf' on FreeBSD
'su' on FreeBSD
Mount an ISO image under FreeBSD
Load the correct sound driver under FreeBSD without knowing what sound card you are using
Simultaneous sound channels on FreeBSD
FreeBSD network stuff
DOS-to-UNIX file conversion
favicon.ico on UNIX
Emacs tips
Command Line Calculator
Save multimedia streams with 'mplayer'
xargs - solution to 'Argument list too long'
Process multiple images from the command line using 'ImageMagick'
Turn the system bell off under X Windows
Process each line in an input file from the command line (or in a shell script)
How to keep a program running in the background using 'nohup'
How to remove symbolic links in the current directory using 'find' and 'rm'
How to remove Emacs backup files in the current directory and all subdirectories using 'find' and 'rm'
How to execute .profile without logging in
Configure X to handle non-English characters
How to move /var to /usr/var

Redirect a web page    (top)
Redirect to another web page
Apache redirect
C redirect
Perl redirect
PHP redirect
HTML redirect
JavaScript redirect

Javascript    (top)
Trim function
Login form
Register form
Popup window

Perl date functions with Date::Calc
Perl date functions with Date::Calc
Here are some date diff functions I find useful.
You need Date::Calc for the script below to work.
It's available on CPAN, or, if you are on FreeBSD, as the port /usr/ports/devel/p5-Date-Calc.


If you need even more date functions, have a look at the bigger Date::Manip module, also available on CPAN, and as the FreeBSD port /usr/ports/devel/p5-Date-Manip.

Download this script
#!/usr/bin/perl -wT
use strict;
use Date::Calc qw(
Add_Delta_YM
Add_Delta_YMD
Today
Week_of_Year
Monday_of_Week
);

my ($Y,$M,$D) = ('','',''); # Original  year,month,day
my ($y,$m,$d) = ('','',''); # Resulting year,month,day

# First and Last in year
($y,$m,$d) = first_in_year();
print "First and last in this year:\t\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_year();
print " - " . format_date($y, $m, $d) . "\n";
($y,$m,$d) = first_in_year(1999);
print "First and last in year 1999:\t\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_year(1999);
print " - " . format_date($y, $m, $d) . "\n";
($y,$m,$d) = first_in_year_delta(2);
print "First and last in the year 2 years ago: " . format_date($y, $m, $d);
($y,$m,$d) = last_in_year_delta(2);
print " - " . format_date($y, $m, $d) . "\n";

print "\n";

# Last years (from today or specific date)
($y,$m,$d) = last_years_from_date(0); 
print "Last year from today:\t\t\t" . format_date($y, $m, $d) . " - " . format_date(Today()) . "\n";
($Y, $M, $D) = (2006, 1, 5);
($y,$m,$d) = last_years_from_date(5, $Y, $M, $D); 
print "Last five years from ". format_date_intl($Y, $M, $D) . ":\t" . format_date($y, $m, $d) . " - " . format_date($Y, $M, $D) . "\n";
($Y, $M, $D) = (2005, 12, 31);
($y,$m,$d) = last_years_from_date(10, $Y, $M, $D); 
print "Last ten years from ". format_date_intl($Y, $M, $D) . ":\t\t" . format_date($y, $m, $d) . " - " . format_date($Y, $M, $D) . "\n";

print "\n";

# First and Last in month
($y,$m,$d) = first_in_month();
print "First and last in this month:\t\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_month();
print " - " . format_date($y, $m, $d) . "\n";
($y,$m,$d) = first_in_month(1999,2);
print "First and last in month 1999-02:\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_month(1999,2);
print " - " . format_date($y, $m, $d) . "\n";
# First and Last in last month
($y,$m,$d) = first_in_last_month();
print "First and last in last month:\t\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_last_month();
print " - " . format_date($y, $m, $d) . "\n";

print "\n";

# Last three calendar months (including current month)
($y,$m,$d) = first_in_month_delta(2);
print "Last three calendar months (including this month):\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_month_delta(0);
print " - " . format_date($y, $m, $d) . "\n";
# Last six calendar months (including current month)
($y,$m,$d) = first_in_month_delta(5);
print "Last six calendar months (including this month):\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_month_delta(0);
print " - " . format_date($y, $m, $d) . "\n";
# Last twelve calendar months (including current month)
($y,$m,$d) = first_in_month_delta(11);
print "Last twelve calendar months (including this month):\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_month_delta(0);
print " - " . format_date($y, $m, $d) . "\n";
# Last three calendar months (excluding current month)
($y,$m,$d) = first_in_month_delta(3);
print "Last three calendar months (excluding this month):\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_month_delta(1);
print " - " . format_date($y, $m, $d) . "\n";
# Last six calendar months (excluding current month)
($y,$m,$d) = first_in_month_delta(6);
print "Last six calendar months (excluding this month):\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_month_delta(1);
print " - " . format_date($y, $m, $d) . "\n";
# Last twelve calendar months (excluding current month)
($y,$m,$d) = first_in_month_delta(12);
print "Last twelve calendar months (excluding this month):\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_month_delta(1);
print " - " . format_date($y, $m, $d) . "\n";

print "\n";

# Last months (from today or specific date)
($y,$m,$d) = last_months_from_date(3);
print "Last three months from today:\t\t" . format_date($y, $m, $d) . " - " . format_date(Today()) . "\n";
($Y, $M, $D) = (2006, 1, 5);
($y,$m,$d) = last_months_from_date(6, $Y, $M, $D);
print "Last six months from ". format_date_intl($Y, $M, $D) . ":\t" . format_date($y, $m, $d) . " - " . format_date($Y, $M, $D) . "\n";
($Y, $M, $D) = (2005, 12, 31);
($y,$m,$d) = last_months_from_date(12, $Y, $M, $D);
print "Last twelve months from ". format_date_intl($Y, $M, $D) . ":\t" . format_date($y, $m, $d) . " - " . format_date($Y, $M, $D) . "\n";

print "\n";

# First and Last in week
($y,$m,$d) = first_in_week(); 
print "First and last in this week:\t\t\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_week();
print " - " . format_date($y, $m, $d) . "\n";
($Y,$M,$D) = (2005,3,1);
($y,$m,$d) = first_in_week($Y, $M, $D);
print "First and last in week for date " . format_date_intl($Y, $M, $D) . ":\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_week($Y, $M, $D);
print " - " . format_date($y, $m, $d) . "\n";
# First and Last in last week
($y,$m,$d) = first_in_last_week();
print "First and last in last week:\t\t\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_last_week();
print " - " . format_date($y, $m, $d) . "\n";

print "\n";

# Last three calendar weeks (including current week)
($y,$m,$d) = first_in_week_delta(2);
print "Last three calendar weeks (including this week):\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_week_delta(0);
print " - " . format_date($y, $m, $d) . "\n";
# Last six calendar weeks (including current week)
($y,$m,$d) = first_in_week_delta(5);
print "Last six calendar weeks (including this week):\t\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_week_delta(0);
print " - " . format_date($y, $m, $d) . "\n";
# Last twelve calendar weeks (including current week)
($y,$m,$d) = first_in_week_delta(11);
print "Last twelve calendar weeks (including this week):\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_week_delta(0);
print " - " . format_date($y, $m, $d) . "\n";
# Last three calendar weeks (excluding current week)
($y,$m,$d) = first_in_week_delta(3);
print "Last three calendar weeks (excluding this week):\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_week_delta(1);
print " - " . format_date($y, $m, $d) . "\n";
# Last six calendar weeks (excluding current week)
($y,$m,$d) = first_in_week_delta(6);
print "Last six calendar weeks (excluding this week):\t\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_week_delta(1);
print " - " . format_date($y, $m, $d) . "\n";
# Last twelve calendar weeks (excluding current week)
($y,$m,$d) = first_in_week_delta(12);
print "Last twelve calendar weeks (excluding this week):\t" . format_date($y, $m, $d);
($y,$m,$d) = last_in_week_delta(1);
print " - " . format_date($y, $m, $d) . "\n";

print "\n";

# Last weeks (from today or specific date)
($y,$m,$d) = last_weeks_from_date(3);
print "Last three weeks from today:\t\t" . format_date($y, $m, $d) . " - " . format_date(Today()) . "\n";
($Y, $M, $D) = (2006, 1, 5);
($y,$m,$d) = last_weeks_from_date(6, $Y, $M, $D);
print "Last six weeks from ". format_date_intl($Y, $M, $D) . ":\t\t" . format_date($y, $m, $d) . " - " . format_date($Y, $M, $D) . "\n";
($Y, $M, $D) = (2005, 12, 31);
($y,$m,$d) = last_weeks_from_date(12, $Y, $M, $D);
print "Last twelve weeks from ". format_date_intl($Y, $M, $D) . ":\t" . format_date($y, $m, $d) . " - " . format_date($Y, $M, $D) . "\n";

print "\n";

# Formatted output
print "Formatted output: " . format_date($y, $m, $d) . "\n";
print "Formatted output: - International: " . format_date_intl($y, $m, $d) . "\n";
print "Formatted output: - Spanish: " . format_date_es($y, $m, $d) . "\n";

# ----------------------------------------
# subs
# ----------------------------------------

# First in year
# Usage: ($y) = first_in_year([$y]);
sub first_in_year 
{
  my ($y,$m,$d) = (scalar(@_) == 1) ? (shift, 1, 1) : Today();
  $m = 1;
  $d = 1;
  return ($y, $m, $d);
}

# Last in year
# Usage: ($y) = last_in_year([$y]);
sub last_in_year 
{
  my ($y,$m,$d) = (scalar(@_) == 1) ? (shift, 1, 1) : Today();
  $m = 12;
  $d = 31;
  return ($y, $m, $d);
}

# First in year - $years years in the past
# Usage: ($y) = first_in_year_delta($years);
sub first_in_year_delta 
{
  my $years = shift;
  my ($y,$m,$d) = Today();
  ($y,$m,$d) = Add_Delta_YM($y, 1, 1, -$years, 0);
  return ($y, $m, $d);
}

# Last in year - $years years in the past
# Usage: ($y) = last_in_year_delta($years);
sub last_in_year_delta 
{
  my $years = shift;
  my ($y,$m,$d) = Today();
  ($y,$m,$d) = Add_Delta_YM($y, 12, 31, -$years, 0);
  return ($y, $m, $d);
}

# Last $years years in the past, counting from a specific date, or today
# Usage: ($y, $m, $d) = last_years_from_date($years);
sub last_years_from_date 
{
  my $years = shift || 1;
  my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today();
  ($y,$m,$d) = Add_Delta_YM($y, $m, $d, -$years, 0);
  ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, 1);
  return ($y, $m, $d);
}

# First in month
# Usage: ($y, $m, $d) = first_in_month([$y, $m]);
sub first_in_month 
{
  my ($y,$m,$d) = (scalar(@_) == 2) ? (shift, shift, 1) : Today();
  $d = 1;
  return ($y, $m, $d);
}

# Last in month
# Usage: ($y, $m, $d) = last_in_month([$y, $m]);
sub last_in_month 
{
  my ($y,$m,$d) = (scalar(@_) == 2) ? (shift, shift, 1) : Today();
  ($y,$m,$d) = Add_Delta_YM($y, $m, 1, 0, 1);
  ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, -1);
  return ($y, $m, $d);
}

# First in last month
# Usage: ($y, $m, $d) = first_in_last_month();
sub first_in_last_month 
{
  my ($y,$m,$d) = Today();
  ($y,$m,$d) = Add_Delta_YM($y, $m, 1, 0, -1);
  return ($y, $m, $d);
}

# Last in last month
# Usage: ($y, $m, $d) = last_in_last_month();
sub last_in_last_month 
{
  my ($y,$m,$d) = first_in_last_month();
  return last_in_month($y, $m);
}

# First in $months months in the past
# Usage: ($y, $m, $d) = first_in_month_delta($months);
sub first_in_month_delta 
{
  my $months = shift;
  my ($y,$m,$d) = Today();
  ($y,$m,$d) = Add_Delta_YM($y, $m, 1, 0, -$months);
  return ($y, $m, $d);
}

# Last in $months months in the past
# Usage: ($y, $m, $d) = last_in_month_delta($months);
sub last_in_month_delta 
{
  my $months = shift;
  my ($y,$m,$d) = first_in_month_delta($months);
  return last_in_month($y, $m);
}

# Last $months months in the past, counting from a specific date, or today
# Usage: ($y, $m, $d) = last_months_from_date($months);
sub last_months_from_date 
{
  my $months = shift || 1;
  my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today();
  ($y,$m,$d) = Add_Delta_YM($y, $m, $d, 0, -$months);
  ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, 1);
  return ($y, $m, $d);
}

# First in week
# Usage: ($y, $m, $d) = first_in_week([$y, $m, $d]);
sub first_in_week 
{
  my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today();
  ($y, $m, $d) = Monday_of_Week(Week_of_Year($y, $m, $d));
  return ($y, $m, $d);
}

# Last in week
# Usage: ($y, $m, $d) = last_in_week([$y, $m, $d]);
sub last_in_week 
{
  my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today();
  ($y, $m, $d) = Monday_of_Week(Week_of_Year($y, $m, $d));
  ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, 6);
  return ($y, $m, $d);
}

# First in last week
# Usage: ($y, $m, $d) = first_in_last_week();
sub first_in_last_week 
{
  my ($y,$m,$d) = first_in_week();
  ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, -7);
  return ($y, $m, $d);
}

# Last in last week
# Usage: ($y, $m, $d) = last_in_last_week();
sub last_in_last_week 
{
  my ($y,$m,$d) = first_in_week();
  ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, -1);
  return ($y, $m, $d);
}

# First in $weeks weeks in the past
# Usage: ($y, $m, $d) = first_in_week_delta($weeks);
sub first_in_week_delta 
{
  my $weeks = shift;
  my ($y,$m,$d) = Today();
  ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, -7 * $weeks);
  ($y,$m,$d) = first_in_week($y,$m,$d);
  return ($y, $m, $d);
}

# Last in $weeks weeks in the past
# Usage: ($y, $m, $d) = last_in_week_delta($weeks);
sub last_in_week_delta 
{
  my $weeks = shift;
  my ($y,$m,$d) = Today();
  ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, -7 * $weeks);
  ($y,$m,$d) = last_in_week($y,$m,$d);
  return ($y, $m, $d);
}

# Last $weeks weeks in the past, counting from a specific date, or today
# Usage: ($y, $m, $d) = last_weeks_from_date($weeks);
sub last_weeks_from_date 
{
  my $weeks = shift || 1;
  my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today();
  ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, -7 * $weeks);
  ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, 1);
  return ($y, $m, $d);
}

# Formatted date (YYYYMMDD - possible zero padding)
# Usage: $string = format_date([$y, $m, $d]);
sub format_date 
{
  my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today();
  return sprintf("%04d%02d%02d", $y, $m, $d);
}

# Formatted date - Intl. (YYYY-MM-DD - possible zero padding)
# Usage: $string = format_date([$y, $m, $d]);
sub format_date_intl 
{
  my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today();
  return sprintf("%04d-%02d-%02d", $y, $m, $d);
}

# Formatted date - ES (DD/MM/YYYY - possible zero padding)
# Usage: $string = format_date([$y, $m, $d]);
sub format_date_es 
{
  my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today();
  return sprintf("%d/%d/%04d", $d, $m, $y);
}
Last modified: Wed Sep 7 12:01:46 EDT 2016