|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
subtract dates
I want to make a "Todays Birthday" type of thing ( like here on Codewalkers ). I'm pulling a date from mysql stored in MM-DD-YYYY format. I want to subtract it from the current date to get the number of years between the 2 dates. The only example I found was like this...
$date = date('m-d-Y'); $x = $date; (for current date) $y = $birthdate; (field from mysql) $age = $x - $y; All I get is a zero. What am I missing? |
|
#2
|
||||
|
||||
|
RE: subtract dates
|
|
#3
|
||||
|
||||
|
RE: subtract dates
sliver's code isn't going to work since subtracting two timestamps doesn't give you a timestamp like you'd want. Since timestamps are based on 0 = 1/1/1970, you'll get something $age years greater than 1970. Even then you might have problems with birthdays before 1970.
Since you're getting the birthdate out of MySQL, you might want to do the age calculation in MySQL to. There are lots of comments on this page about method to calculate age. |
|
#5
|
|||
|
|||
|
RE: subtract dates
This is what I set up, however it isn't showing any results. The field names are "fname" and "birthdate", table name is "fpc_birthday".
php connection string $today = date("Y-m-d"); $result=mysql_query("SELECT fname,YEAR(birthdate) FROM fpc_birthday WHERE MONTH(birthdate)=MONTH('$today') AND DAYOFMONTH(birthdate)=DAYOFMONTH('$today')"); while($row = mysql_fetch_array($result)) { $userName = $row[0]; $userAge = date("Y")-$row[1]; echo "$userName [$userAge]<br>$today"; } /php I added the $today in the echo, even this does not display unless I move the echo outside of the ending "}" bracket, then all that displays is this... [] 2004-01-04 I also changed userName to fname just in case...nothing is showing up. I'm sure I', missing something fairly simple but I don't know what it is. |
|
#6
|
|||||
|
|||||
|
RE: subtract dates
Try this. Are you sure you have records which has the birthdays of today? Above code worked very well in my site. I feel strange in this case!
php Code:
|
|
#7
|
|||
|
|||
|
RE: subtract dates
The only thing showing is...
There are 0 birthdays today. There are 3 entries with todays date... 01-04-1960 01-04-1990 01-04-2004 I know the field names and table name are correct, as well as the db connection. Does the order of d-m-y make any difference? |
|
#8
|
|||
|
|||
|
RE: subtract dates
It makes a difference, but not in this SQL query. This is strange, I used this same code in my site and worked well.
|
|
#9
|
|||
|
|||
|
RE: subtract dates
|
|
#10
|
|||
|
|||
|
RE: subtract dates
This is something I'm testing for a program I'm putting together, and there are only 5 entries in this table so far. This still brings up no results. I hard coded the user/pass/db into the connection string to make sure there were no problems. If I do the following, it pulls the information fine, but displays all birthdays in the table, not the ones for just today.
php include("connect.php"); $conn = mysql_connect(localhost,"$dbuser","$dbpass") or die ("Could not connect MySQL"); mysql_select_db("$db") or die ("Could not open database"); $query = ("SELECT * FROM fpc_birthday ORDER BY birthdate ASC"); $result = mysql_query($query); $num=mysql_num_rows($result); $i = 0; while ($i < $num): $id=mysql_result($result,$i,"id"); $fname=mysql_result($result,$i,"fname"); $birthdate=mysql_result($result,$i,"birthdate"); echo "$id, $fname, $birthdate<br>"; $i++; endwhile; /php |
|
#11
|
|||
|
|||
|
RE: subtract dates
I guessed that the problem was with your database connection, but you told that you are
really sure with that one! surprising as I cannot see any condition to check birthdays only for today. You are grabbing all the records from the table and just displaying it. That causes your problem! Now run this script: |
|
#12
|
|||
|