Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1942 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Sorting by age issue.

Thread title: Sorting by age issue.
Closed Thread    
    Thread tools Search this thread Display Modes  
07-31-2008, 05:09 PM
#1
Jako is offline Jako
Jako's Avatar
Status: Jakowenko.com
Join date: Jun 2005
Location: Michigan
Expertise:
Software:
 
Posts: 2,199
iTrader: 3 / 100%
 

Jako is on a distinguished road

  Old  Sorting by age issue.

I have a table that stores users birthdays, date, month year, etc.

I then have a function which turns these into an age, I.E. 24.

Problem is when I want to show all users who are a specific age I get stuck. I can do it an easy way and sort by their year born, but this will sometimes show a fluctuation because everyone born from 1988 may not be 20 yet.

Is there any work around for this?

07-31-2008, 09:09 PM
#2
infinivert is offline infinivert
infinivert's Avatar
Status: Junior Member
Join date: Jul 2008
Location: Abilene TX
Expertise: Design, PHP, JS, HTML5, CSS3
Software:
 
Posts: 37
iTrader: 0 / 0%
 

infinivert is on a distinguished road

Send a message via AIM to infinivert

  Old

Sure, it just requires a slightly more complicated SQL statement that converts the date or datetime field to a Unix timestamp. I would probably do something like this:
PHP Code:
$age 20 // change this variable to get different ages

$age_sql "SELECT * FROM tblTableName WHERE UNIX_TIMESTAMP(fldBirthDate) >= UNIX_TIMESTAMP(NOW())-" $age 31556926 " AND UNIX_TIMESTAMP(fldBirthDate) < UNIX_TIMESTAMP(NOW())-" . ($age 1) * 31556926 ";"
...where tblTableName is the table and fldBirthDate is the field containing your birthdates.

A Unix timestamp is the number of seconds since Jan. 1, 1970. 31556926 is the number of seconds in 1 year (365.24 days).

I haven't tested this explicitly, so no guarantees, but I think it gets the general idea across. And I'm sure there may be a more eloquent way to do this, so if anybody knows one, please share!

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed