Hi, i recently found a script which contains an array of peoples birthdays and displays them when their birthdays are. I have a slight problem... My website is in php and i have a mysql database. I need the script to be in php and the array to be taken from a mysql database or from a flat file (preferably the database 😉 ).
Also, if its not too much trouble, can someone please make it display the persons age?
Well here is the script anyway, i'll include another one with the age variable if it is any help.
<html>
<head>
<title>Birthday List</title>
<script type="text/javascript">
//the bday array data can be generated from server-side
var arrBday = [
['John Doe', '5/6'],
['John Doe1', '5/31'],
['John Doe2', '5/30'],
['John Doe3', '5/28'],
['John Doe4', '5/27'],
['John Doe5', '5/18'],
['John Doe6', '5/20'],
['John Doe7', '5/19']
//...and so on (last entry must not have a trailing comma)
];
function getBdaysThisWeek(){
var arrMonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var bday, idx;
var bdayList = new Array();
var today = new Date();
for (var i=0;i<arrBday.length;i++){
var bday = new Date(arrBday[i][1] + '/' + today.getFullYear());
if (isNaN(bday)) continue;
if ( isBdayInRange(bday, 7) ){
idx = bdayList.length;
bdayList[idx] = new Object();
bdayList[idx].name = arrBday[i][0];
bdayList[idx].bday = bday;
bdayList[idx].month = arrMonth[bday.getMonth()];
}
}
if (bdayList.length > 0){ //sort asc by birthdate
bdayList.sort(
function(a, b){
if (a.bday < b.bday) return -1
if (a.bday > b.bday) return 1;
return 0;
}
);
}
return bdayList;
}
function isBdayInRange(bday, interval){
//credit for this function goes to:
//-Rob (@slingfive) Eberhardt, Slingshot Solutions
//http://slingfive.com/pages/code/jsDate/jsDate.html
var today = new Date();
//have to override time so entire day will be valid
today.setHours(0,0,0,0);
//if the birthday has already occurred in the year, increment to the next year
if (bday < today)
bday.setFullYear(bday.getFullYear() + 1);
// get ms between dates (UTC) and make into "difference" date
var iDiffMS = bday.valueOf() - today.valueOf();
//divide iDiffMS by 1000, Seconds, Minutes, Hours
nDays = parseInt(iDiffMS / 1000 / 60 / 60 / 24);
if(parseInt(nDays) <= parseInt(interval))
return true;
else
return false;
}
function displayBdayList(){
var date = new Date().getDate();
var bdayList = getBdaysThisWeek();
var len = bdayList.length;
var s = "<h1>Birthday Celebrants for this week:</h1>";
if (len>0){
s += '<ul>';
for (var i=0; i<len; i++){
//be mindful of the string-line continuation character (\) at the end of the first line
s += '<li' + ((date == bdayList[i].bday.getDate())?' class="bdayToday"':'')+ '>\
<strong>' + bdayList[i].name + '</strong> - '
+ bdayList[i].month + ' ' + bdayList[i].bday.getDate() + '</li>';
}
s += '</ul>';
}
else{
s += "No birthday celebrant for this week.";
}
document.write(s);
}
</script>
<style type="text/css">
body {
font:14px Verdana;
}
/*display style when bday is today*/
.bdayToday {
color: red;
}
</style>
</head>
<body>
<script type="text/javascript">
displayBdayList();
</script>
</body>
</html>
Here's the one with the age but only displays on day not in week.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Birthday List</title>
</head>
<body>
<script language="JavaScript">
var arrBday = [
['Bob','11/23/1973'],
['Peter','5/20/1977'],
['John','9/22/1999']
];
function displayBdayList(today){
var bday,strList='';
for (var i=0;i<arrBday.length;i++){
bday = new Date(arrBday[i][1]);
if (!isNaN(bday) && bday.getMonth()==today.getMonth() && bday.getDate()==today.getDate())
strList+='- '+arrBday[i][0]+" ("+(today.getFullYear()-bday.getFullYear())+")<br>";
}
if (strList=='') strList='- NONE'
document.write("<h4>Today's Birtdays:</h4>"+strList)
}
displayBdayList(new Date());
</script>
</body>
</html>
Thanx, Seanko