Hi Guys,
I just want a share a script which I have made.
The script is meant to return a date based on the day and occurrences.
So, for example this is the application of the script:
1. you want to get the date for first Friday of the current month
2. you want to know whether there is a 5th week Monday.
The first input is the day (0 - 6 where 0 is Sunday)
The second input is the occurence (1 - 5)
here goes my script:
PHP Code:
#!/bin/bash
# This script is used to find the date of certain day in certain week
# for example:
# first Monday of the month
# second Wednessday of the month
# first day of the month
# for example 1st March 2012 is Thursday
# this will give result of 4
firstdom=$(date -d "-$[`date +%d` -1] days" +%w)
#last date of the month
lastdom=$(date -d "-$[`date +%d` -1] days + 1 month - 1 days" +%d)
# last date of first week
w1=$[7-$(date -d "-$[`date +%d` -1] days" +%w)]
# d is the date parameter
# w is the occurrence parameter
d=$1
w=$2
if [ $d -lt $firstdom ]; then
r=$(( (7-$firstdom)+(7*($w-1))+$d ))
if [ $(( $r+1 )) -gt $lastdom ]; then
exit 1;
else
echo `date -d "-$[ $(date +%d) -1] days + $r days" +%Y%m%d`
fi
fi
if [ $d -ge $firstdom ]; then
r=$(( ($d-$firstdom)+(7*($w-1)) ))
if [ $(( $r+1 )) -gt $lastdom ]; then
exit 1
else
echo `date -d "-$[ $(date +%d) -1] days + $r days" +%Y%m%d`
fi
fi
let's say I named my script as wom.sh
Then this is how I use it
Show the date of the second Monday of current month
$./wom.sh 1 2
result: 20120409
please let me know your comments.
Thank you,
Adhika