LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-02-2012, 05:31 AM   #1
rexus
LQ Newbie
 
Registered: Apr 2010
Posts: 10

Rep: Reputation: 0
[Sharing] Get days occurrence each month


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 -"-$[`date +%d` -1] days" +%w)

#last date of the month
lastdom=$(date -"-$[`date +%d` -1] days + 1 month - 1 days" +%d)

# last date of first week
w1=$[7-$(date -"-$[`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+)) -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+)) -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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find file older than one month not by x days olds jao_madn Programming 2 12-03-2011 03:06 PM
How to print out "n" days ago's year, month and day? ArthurHuang Programming 3 02-10-2011 09:33 PM
LXer: CodeWeavers CEO names himself employee of the month for 175TH consecutive month LXer Syndicated Linux News 3 08-13-2010 05:55 PM
PHP Days of the month counter. waelaltaqi Programming 8 10-22-2007 10:51 AM
Calculating age in days and month in a bash script jachba Programming 5 06-23-2006 02:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration