Welcome Guest, Not a member yet? Register   Sign In
sql query for days until next birthday?
#5

[eluser]sophistry[/eluser]
i got back to this after a while because there was a bug in the original SQL.

this is much clearer. thanks for the idea to use month and month day comparison instead of day of year.
Code:
// use the second param of select to not escape this string                        
$Ymd = "'".date('Y-m-d')."'";
$this->db->select("DATEDIFF(
                IF( (MONTH(datetime_start) >= MONTH($Ymd)) AND
                     (DAYOFMONTH(datetime_start) >= DAYOFMONTH($Ymd)),
                    CONCAT_WS('-',YEAR($Ymd),MONTH(datetime_start),DAYOFMONTH(datetime_start)),
                    CONCAT_WS('-',YEAR($Ymd)+1,MONTH(datetime_start),DAYOFMONTH(datetime_start))
                ),
                $Ymd
            )                
            AS days_until_event", FALSE);

this works really well for any kind of year-bounded countdown!


Messages In This Thread
sql query for days until next birthday? - by El Forum - 02-06-2009, 11:33 AM
sql query for days until next birthday? - by El Forum - 02-06-2009, 12:48 PM
sql query for days until next birthday? - by El Forum - 02-06-2009, 12:58 PM
sql query for days until next birthday? - by El Forum - 02-06-2009, 01:18 PM
sql query for days until next birthday? - by El Forum - 10-01-2009, 02:12 PM
sql query for days until next birthday? - by El Forum - 10-19-2009, 08:03 PM



Theme © iAndrew 2016 - Forum software by © MyBB