Jump to content

[SOLVED] Help me understand what these codes mean.


Scorptique

Recommended Posts

Hey all, I need help understand the codes. This set of codes work, they basically display the upcoming birthdays from my contact list.

 

$sql = "select cl_id, cl_fname, cl_lname, cl_email, date_format(cl_dob, '%d %b %Y') AS cl_dob,
(TO_DAYS((cl_dob + INTERVAL (YEAR(CURRENT_DATE)-YEAR(cl_dob) + (RIGHT(CURRENT_DATE,5)>
RIGHT(cl_dob,5))) YEAR)) - TO_DAYS(CURRENT_DATE)) as daysAway from contact_list where
username = '$username' order by daysAway DESC";

 

But...i dont really understand (TO_DAYS((cl_dob + INTERVAL (YEAR(CURRENT_DATE)-YEAR(cl_dob) + (RIGHT(CURRENT_DATE,5)>RIGHT(cl_dob,5))) YEAR)) - TO_DAYS(CURRENT_DATE)). if you understand, can you explain to me please? Thanks! ^^;;

Hm, I understand the individual functions, but I can't seem to comprehend how it works as a whole...especially for this part.

 

(TO_DAYS((cl_dob + INTERVAL (YEAR(CURRENT_DATE)-YEAR(cl_dob) + (RIGHT(CURRENT_DATE,5)>
RIGHT(cl_dob,5))) YEAR)) - TO_DAYS(CURRENT_DATE))

So let's take it from outside to inside

 

If you drop outermost parentheses (), you will see, that there are two TO_DAYS() functions

 

TO_DAYS((cl_dob....)  -  TO_DAYS(CURRENT_DATE)

 

So, we have a number of days since year 0 calculated for the some date in the first function, and then number of days since year 0 for today substracted from it.

So basically it is a number of days between tha date in first function, and today.

 

Let's go some deeper into this first TO_DAYS() function

 

cl_dob + INTERVAL (YEAR(CURRENT_DATE)-YEAR(cl_dob) + (RIGHT(CURRENT_DATE,5)>RIGHT(cl_dob,5))) YEAR

 

So we now have two expressions added together.

 

cl_dob

This comes from table

 

INTERVAL (YEAR(CURRENT_DATE)-YEAR(cl_dob) + (RIGHT(CURRENT_DATE,5)>RIGHT(cl_dob,5)))

 

This one is strange, because INTERVAL() should have at least two arguments... you sure this is right?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.