Jump to content

Embarrassing situation and getting hate mail - HELP


Recommended Posts

I decided to teach myself SEO and PHP MYSQL. I started with a topic that I thought would come up - Politicks.

I am suddenly getting over 10 - 12,000 page views a day.  One of my pages has the ages of the presidential candidates and it's screwed up.  I tried for hours - here it is. 

http://www.presidential-candidates.org/compare-presidential-candidates-age.php

 

  $born=$record["born"];

  $age=(date("Y")-"$born");

  $birthdate=(date("$born","l, F j, Y"));

 

I can't get the age to come up correctly (only the year),  I can't get the date to format properly either.  Help Please

 

I have received numerous mail concerning this problem, here is the last one.... My math is fine. The code leaves something to be desired.

 

If you can't do simple math, how can we trust your information? Check the

ages you list for Clinton and Obama. Shame on you!!!

 

EMJ876@bellsouth.net

Marietta, GA

 

Is this what you mean???

 

<?

include_once "shared/menu/presidential-candidates-left.php";

?>

    </td>

   

    <td valign="top">

      <div align="center"> <br>

        <font size="6" face="Times New Roman, Times, serif"> Presidential Candidate

        Comparison</font><br>

        <font color="#003399" size="6" face="Arial, Helvetica, sans-serif"><strong>

        Compare Candidate Ages <br>

        </strong></font><font size="3" face="Arial, Helvetica, sans-serif">in

        order from youngest to oldest</font></div>

        <table width="600" align="center" cellpadding="5" cellspacing="0">

          <?

include_once "database.php";

 

$query=mysql_query("SELECT * FROM  political_candidates WHERE status = 'active' ORDER BY born DESC ");

 

?>

          <?

while ($zaznam=mysql_fetch_array($query))

  {

  $last_name=$zaznam["last_name"];

  $first_name=$zaznam["first_name"];

  $nick_name=$zaznam["nick_name"];

  $political_party=$zaznam["political_party"];

  $born=$zaznam["born"];

  $borntown=$zaznam["borntown"];

  $hometown=$zaznam["hometown"];

  $born=$zaznam["born"];

  $age=(date("Y")-"$born");

  $birthdate=(date("$born","dS F Y"));

 

        // Get status color 

          if($political_party == "Democratic") {

            $color = "#002266";

        }

        if($political_party == "Republican") {

            $color = "#CC0000";

        }

        if($political_party == "Independent") {

            $color = "#006600";

        }

 

  echo"<TD style=\"text-align: center;  width=100; background-color:'$color'\"><img src=http://www.presidential-candidates.org/candidates/images/thumb.php?src=".$last_name.".jpg&x=120&y=120&f=0.jpg height=120 width=90 border=2 alt=2008 Republican Presidential Candidate Ages></TD>

<TD><br>

<center><font size=2><strong>".$political_party." Presidential Candidate</strong></font>

<br>

<center><font size=4><strong>".$nick_name." ".$last_name." Age</strong></font>

<br>

<div align=left><pre><font face=Arial, Helvetica, sans-serif><font size=2> &#8226; Birth Date: ".$birthdate."</font></div><br>

<div align=left><pre><font face=Arial, Helvetica, sans-serif><font size=2> &#8226; Age: ".$age."</font></div>

<hr></center>

</TD>

</TR>

";}

?>

But a simply suggestion would be to do this, supposing your $record['born'] holds a unix timestamp:

 

change

 

   $born=$zaznam["born"];
   $age=(date("Y")-"$born");
   $birthdate=(date("$born","dS F Y"));

 

to

 

$born = $zaznam["born"];
$age = date("Y") - date("Y",$born);
$birthdate = date("dS F Y",$born);

 

It looks like you used wrong syntax for date()... http://php.net/date

 

And generally some funny syntax :P

Excuse me, I'm a tenderfoot here.  First, 'zaznam' is Czech for 'record'.

 

  $born=$zaznam["born"];

  $age=(date("Y")-"$born");

  $birthdate=(date("$born","l, F j, Y"));

 

The

We need to know what the variable $record["born"] holds.... is it a unix timestamp or?

 

The record is just the date value and I enter it as 1942-11-20.

 

 

Wuhtzu,  I tried the code but everything came up 1-1-1970

 

The problem with age is, you cannot simply subtract the years to get it. If your birthday has not occurred yet in the current year, the calculation says you are older than you really are. You must take into account the current month and the month of the birthday.

 

The mysql manual has an example of calculating age, given a DATE (yyyy-mm-dd) -

 

mysql> SELECT name, birth, CURDATE(),
    -> (YEAR(CURDATE())-YEAR(birth))
    -> - (RIGHT(CURDATE(),5)<RIGHT(birth,5))
    -> AS age
    -> FROM pet;

Well the main problem was / is markts trying to subtract yyyy-mm-dd ($record['born']) from a unix timestamp (date("Y")).... the lesser problem is which day of the year your birthday is :)

 

But since your $zazman['born'] is a yyyy-mm-dd date you should use mysql to do the work as PFMaBiSmAd suggests.

<?php
$born = $zaznam["born"];
$age = date("Y") - date("Y",$born);
$birthdate = date("j m Y",$born);
?>

 

Man, this didn't work.  I tried the age line and the birthdate line seperately and all I got was 38 years old and/or 01.01.1970

 

 

PFMaBiSmAd , I don't quite understand this yet - Where does RIGHT come in.  THanks for trying to help.  Guess it's time to study the manual some more. 

 

mysql> SELECT name, birth, CURDATE(),

    -> (YEAR(CURDATE())-YEAR(birth))

    -> - (RIGHT(CURDATE(),5)<RIGHT(birth,5))

    -> AS age

    -> FROM pet;

Firstly, most (all) the various pieces of PHP code that have been posted are operating on and comparing incompatible values. The value from the database is a DATE type - yyyy-mm-dd.

 

This is the part of that query that matters -

 

(YEAR(CURDATE())-YEAR(birth)) - (RIGHT(CURDATE(),5)<RIGHT(birth,5)) AS age

 

Here is what this does -

 

Get the year of the current date minus the year of the birth date and subtract the result of (mm-dd part of the current date < mm-dd part of the birth date)

 

(mm-dd part of the current date < mm-dd part of the birth date) will either be TRUE (one) or FALSE (zero). So this comparison returns either a one (if the birthday has not gone past) or a zero (if the birthday is the current date or has already gone past). This is subtracted from the difference in years, giving the true age.

 

Try This:

<?php
$born = $zaznam["born"];
$age =  date("Y",$born);
$birthdate = date("j m Y");
echo "Age:";
echo ''. round(dateDiff("/", date("j/m/Y", time()), $birthdate)/365, 0). 'years';

function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}


?>

<?php
$born = $zaznam["born"];
$age =  date("Y",$born);
$birthdate = date("j m Y");
echo "Age:";
echo ''. round(dateDiff("/", date("j/m/Y", time()), $birthdate)/365, 0). 'years';

function dateDiff($dformat, $birthdate, $currentdate)
{
$date_parts1=explode($dformat, $currentdate);
$date_parts2=explode($dformat, $birthdate);
$start_date=($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $currentdate - $birthdate;
}


?>

The date() function requires a UNIX Timestamp. That is not what is in $zaznam["born"].

 

Also, the date() function converts to the current timezone setting on the server, so using the date() function for things like birthdays can yield a wrong age when the current date is with in a day of the birthday.

 

To format a mysql DATE (yyyy-mm-dd) in any format you want, simply use the mysql DATE_FORMAT() function in the SELECT query - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format

 

No complicated PHP code is required.

Hey All.  I want to thank you for all the suggestions.  I tried all and started a new page copy not to disrupt the site. http://www.presidential-candidates.org/age.php Between trials and the manual I've been working hours on it now and it's after midnight. Got an early start tomorrow, will come back  thanks

Because of leap years (every 4 years there are 366 days), knowing or using the number of days between dates or simply dividing anything by 365 will not give the correct age when the current date is with in the number of extra days caused by leap years to the birthday.

 

Your age is defined as the difference between the current year and your year of birth if you birthday has already occurred in the current year, subtract one from that value if your birthday has not occurred yet in the current year. See the formula I posted above.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.