pepe
pepe

Reputation: 9909

Select next event from 2 arrays

I have 2 tables

table 'events'
id      event_name    date           
1       buy milk      1319074920 // 10-20-2011
2       lunch         1321753320 // 11-20-2011

table 'people'
id      user       birthday
1       Jack       16508520 // 7-11-1970
2       Bill       180409320 // 9-20-1975

What I am trying to do is:

[1] create an array with the event closest to current date/time >> SOLVED

My query generates an array like this

Array ( [0] => 
    Array ( 
         [date]       => 1319074920
         [event_name] => buy milk
    ) 
)

[2] create an array with the birthday closest to current date/time

Not sure how to do this. I could query the database and retrieve users and birthdays and loop through the results, but would need to change the birthday year to the current year, keeping month and day unchanged.

[3] compare the 2 arrays and pick one entry that is closest to current date/time: birthday or event.

Any suggestions?

QUERY CODE

    $query = $this->db->query("
    SELECT *
    FROM   events
    WHERE  UTC_1 >= UNIX_TIMESTAMP()
    ORDER  BY UTC_1
    LIMIT  1
    ;");

Upvotes: 0

Views: 89

Answers (1)

xthexder
xthexder

Reputation: 1565

If you've got a query that gives you the closest event, then just use the same query for birthdays, since your tables are the same structure. Once you have the closest event and closest birthday, you can do a simple compare in php to find which one to display.

if (abs(bday[0]['date'] - time()) < abs(event[0]['date'] - time())) {
    // Show birthday
} else {
    // Show event
}

Upvotes: 2

Related Questions