Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


  Search this Thread
Old 04-02-2012, 04:31 AM   #1
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:

# 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

if [ $d -lt $firstdom ]; then
=$(( (7-$firstdom)+(7*($w-1))+$d ))
   if [ $(( 
$r+)) -gt $lastdom ]; then 
exit 1;
      echo `
date -d "-$[ $(date +%d) -1] days + $r days" +%Y%m%d`

if [ $d -ge $firstdom ]; then
=$(( ($d-$firstdom)+(7*($w-1)) ))
   if [ $(( 
$r+)) -gt $lastdom ]; then
exit 1
     echo `
date -d "-$[ $(date +%d) -1] days + $r days" +%Y%m%d`
let's say I named my script as
Then this is how I use it

Show the date of the second Monday of current month
$./ 1 2
result: 20120409

please let me know your comments.

Thank you,


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 02:06 PM
How to print out "n" days ago's year, month and day? ArthurHuang Programming 3 02-10-2011 08:33 PM
LXer: CodeWeavers CEO names himself employee of the month for 175TH consecutive month LXer Syndicated Linux News 3 08-13-2010 04:55 PM
PHP Days of the month counter. waelaltaqi Programming 8 10-22-2007 09:51 AM
Calculating age in days and month in a bash script jachba Programming 5 06-23-2006 01:37 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:04 AM.

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