Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Date Question
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Date Question

 Post Reply Post Reply Page  12>
Author
bootcom View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 February 2005
Location: United Kingdom
Status: Offline
Points: 259
Post Options Post Options   Thanks (0) Thanks(0)   Quote bootcom Quote  Post ReplyReply Direct Link To This Post Topic: Date Question
    Posted: 23 May 2005 at 7:58pm
Hi guys,

Got a slight query, and Im looking for opinions on working out how long  user has been online.

I have remodelled the entire forums to run from arrays, as I am using Access I need all the performance boosters I can get.

Where I am needing my advise is when coming to the new profile page (That I currently have within development) I want the page to show how many years, months, days the user has been a member with me. Now the process i have been using is extremely long winded and I would like someone to suggest a simplified version of how to do it.

It would appear like this on the page:

This user has been a member for x years, y months and z days

Any takers?
Back to Top
Phat View Drop Down
Senior Member
Senior Member


Joined: 23 February 2003
Status: Offline
Points: 386
Post Options Post Options   Thanks (0) Thanks(0)   Quote Phat Quote  Post ReplyReply Direct Link To This Post Posted: 23 May 2005 at 9:31pm
use datediff http://www.devguru.com/Technologies/vbscript/quickref/datediff.html

then check for the number of years, months and days.
Back to Top
ub3rl337ch3ch View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 February 2005
Location: Australia
Status: Offline
Points: 341
Post Options Post Options   Thanks (0) Thanks(0)   Quote ub3rl337ch3ch Quote  Post ReplyReply Direct Link To This Post Posted: 23 May 2005 at 9:42pm
To start with you'll want to add a date/time field to the user table (where login/contact details and such are kept) where the default value is now()
 
You could then use the DateDiff function, but that can only return the number of days OR months OR years between two dates, so you'll end up with 7839 days or something ludicrous, so you have to be creative... or get someone else to be creative for you.
 
<%
Dim YearsSince, MonthsSince, DaysSince, JoinDate, JoinDay, NowDay
sql = "SELECT JoinDate FROM tblUser WHERE UserID=" & [whatever]
vrec.open sql,adoCon
JoinDate = vrec("JoinDate")
vrec.close
 
YearsSince = DateDiff(yyyy,JoinDate,Date)
MonthsSince = DateDiff(m,JoinDate,Date)
'this provides the total months, so we have to account for the number of years that have gone past
MonthsSince = MonthsSince - 12*YearsSince
'if the difference in months is negative then even though the datediff for years may be 1, it is still less than a year between them
if MonthsSince < 0 then
YearsSince = YearsSince - 1
MonthsSince = MonthsSince * -1
end if
'Now we use datepart to wrok out the days
JoinDay = DatePart("d",JoinDate)
NowDay = DatePart("d",Date)
DaysSince = NowDay - JoinDay
'the same applies with any difference in days as difference in months
if DaysSince < 0 then
MonthsSince = MonthsSince - 1
'here you have to account for months of different length, based on whether the month part of the join date is 4,6,9,11 or 1,3,5,7,8,10,12 or 2. This i'm still working on, and i'll post if i can work it out
end if
 
response.write "This user has been on for " & YearsSince & " years, " & MonthsSince & " months and " & DaysSince & " days."


Edited by ub3rl337ch3ch - 23 May 2005 at 9:45pm
Back to Top
bootcom View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 February 2005
Location: United Kingdom
Status: Offline
Points: 259
Post Options Post Options   Thanks (0) Thanks(0)   Quote bootcom Quote  Post ReplyReply Direct Link To This Post Posted: 23 May 2005 at 9:51pm
Phat, thanks for your reply dude. That's how I began working it out, sorry I declined to mention that. I was looking at suggestions at improving my current functionality which is as ub3rl337ch3ch is doing ends up being the same methodology as the one Im currently using. Good to know Im on the right lines though chaps !!
Back to Top
ub3rl337ch3ch View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 February 2005
Location: Australia
Status: Offline
Points: 341
Post Options Post Options   Thanks (0) Thanks(0)   Quote ub3rl337ch3ch Quote  Post ReplyReply Direct Link To This Post Posted: 23 May 2005 at 10:25pm
aside from the above, all you can really do is work out the datediff then work out how many lots of 365 go into each, so you find
 
x = datediff("yyyy",JoinDate,Date)
y = datediff("m",JoinDate,Date) mod 12
z = datediff("d",JoinDate,Date) mod 365
z = z mod 12
 
but then you still have to account for differences in month and year length, and you still have to determine whether
 
i think the method you're using is the best you can hope for, unless someone knows of some groovy other function... :p
 
 
Back to Top
bootcom View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 February 2005
Location: United Kingdom
Status: Offline
Points: 259
Post Options Post Options   Thanks (0) Thanks(0)   Quote bootcom Quote  Post ReplyReply Direct Link To This Post Posted: 23 May 2005 at 10:35pm
That still wouldn't really give me what I needed though mate ....

I played about with what I was doing and came up with a function, just tested it and works great although it is a bit long winded....

What ya reckon


Private Function tellMeTheDMY(dateJoined)

Dim totalYears
Dim totalMonths
Dim totalDays
Dim splitDate
Dim tempDate
Dim tempNum

' Get the total years this user has been a member
totalYears = dateDiff("yyyy", dateJoined, date())

' Now split the date joined variable
splitDate = split(dateJoined, "/")

' Now create a temporary date with the current year in there
tempDate = splitDate(0) & "/" & splitDate(1) & "/" & year(now())

' Now if the tempDate is before the current date we know for definate that they have been here
' for totalYears years (if that makes sense) If not subtract a year.
If cDate(tempDate) > date() then totalYears = totalYears - 1

' Now we need to get the total amount of months this user has been a member
totalMonths = DateDiff("m", dateJoined, date())

' If this user has been a member for over one year then obviously there will be some confusion
' setting in as the amount of months will be over 12 (which isn't practicle for our needs)

' So what we need to do now is calculate the amount of months the user has actually been with us
' For each year the user has been a member take away 12 months from the value
If totalMonths > 12 then
Do until totalMonths < 12
totalMonths = totalMonths - 12
Loop
End If

' Now to work out the days the user has been signed up to the site

tempNum = (totalYears * 12) + totalMonths

' First of all using the months/years the user has been on the site and add them together
' to form a date
tempDate = DateAdd("m", tempNum, dateJoined)

' Now work out the amount of days between the tempdate variable and the current date
totalDays = DateDiff("d", tempDate, now())

tellMeTheDMY = "This user has been a member for " & totalYears & " and " & totalMonths & " and " &totalDays

End Function

Back to Top
ub3rl337ch3ch View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 February 2005
Location: Australia
Status: Offline
Points: 341
Post Options Post Options   Thanks (0) Thanks(0)   Quote ub3rl337ch3ch Quote  Post ReplyReply Direct Link To This Post Posted: 23 May 2005 at 11:02pm
you could use dateadd for the entire thing:
 
x = datepart("yyyy", joindate)
x = dateadd("yyyy",-x,Date)
totalYears = Datepart("yyyy",x)
 
x = datepart("m", joindate)
x = dateadd("m",-x,Date)
if x <0 then
totalYears = totalYears - 1
x = 12 + x
end if
totalMonths = Datepart("m",x)
 
and then use your last bit for working out the day... that's the best way i can see to avoid having to calculate leap years etc.
 
I know its just more of the same, but its a different way, and you way want to just test for speed... this one might be faster by virtue of avoiding loops... not sure which functions are faster to process.
Back to Top
bootcom View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 February 2005
Location: United Kingdom
Status: Offline
Points: 259
Post Options Post Options   Thanks (0) Thanks(0)   Quote bootcom Quote  Post ReplyReply Direct Link To This Post Posted: 23 May 2005 at 11:06pm
Ok now dont ask me why lol but for some reason the


totalDays = DateDiff("d", tempDate, now())


Needs to be specified whether it is after / before the current day number other wise it spits out minuses and allsorts Dead

So by putting in an extra couple of lines here:


If day(tempdate) < day(date()) then
totalDays = DateDiff("d", tempDate, date())
Else
totalDays = DateDiff("d", date(), tempDate)
End If


The code still does what it's employed to do and now remains error free.

Will give your way a go though, it's always proper helpful to get second and third opinions doing these things so thanks for bearing with me. Will get a performance test done on both and see which comes out faster.
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.