views:

52

answers:

1

Hi,

I have two users in my database whose birth date is set to:

1985-01-26

And then i have function which when provided the users' date, tells how many days are left in the birthday. Here is the function:

function retage($iy,$im,$id)
{
    if(!empty($iy)>0 && intval($im)>0 && intval($id)>0)
    {
        $tdo=$iy.'-'.$im.'-'.$id;
         $tdc=date('Y').'-'.$im.'-'.$id;
        /*echo "<br/>";*/
         $cd=date('Y-n-j');
        /*echo "<br/>";*/
        if(strtotime($tdc)>strtotime($cd))//coming
        {
            $ty=floor((strtotime($tdc)-strtotime($tdo))/(3600*24*365));
            $td=floor((strtotime($tdc)-strtotime($cd))/(24*3600));
            if($td==1)
            {
                $td=round((strtotime($tdc)-strtotime($cd))/(24*3600)).' day to go';
            }
            else
            {
                $td=round((strtotime($tdc)-strtotime($cd))/(24*3600)).' days to go';
            }
            $ty='<font color="#C7C5C5">is turning '.$ty.' on <br>'.date('M jS Y',strtotime($tdc)).'</font>';
            //return 'is turning '.$ty.' on '.$tdc;
        }
        elseif(strtotime($tdc)<strtotime($cd))//past
        {
            $ty=floor((strtotime($tdc)-strtotime($tdo))/(3600*24*365));
            if($ty>0)
            {
                //$td='gone '.floor((strtotime($cd)-strtotime($tdc))/(24*3600)).' days ago';

                $ndays=floor((strtotime($cd)-strtotime($tdc))/(24*3600));

                if($ndays==1)
                    $td=' gone '.round((strtotime($cd)-strtotime($tdc))/(24*3600)).' day ago';
                else
                    $td=' gone '.round((strtotime($cd)-strtotime($tdc))/(24*3600)).' days ago'; 

                $ty='<font color="#C7C5C5">had turned '.$ty.' on <br>'.date('M jS Y',strtotime($tdc)).'</font>';
                //return 'had turned '.$ty.' on '.$tdc;
            }
            else
            {
                $tdc=(date('Y')+1).'-'.$im.'-'.$id;
                $ty=floor((strtotime($tdc)-strtotime($tdo))/(3600*24*365));
                //$td=floor((strtotime($tdc)-strtotime($cd))/(24*3600)).' days to go';
                $td=floor((strtotime($tdc)-strtotime($cd))/(24*3600));
                if($td==1)
                {
                    $td=round((strtotime($tdc)-strtotime($cd))/(24*3600)).' day to go';
                }
                else
                {
                    $td=round((strtotime($tdc)-strtotime($cd))/(24*3600)).' days to go';
                }

                $ty='<font color="#C7C5C5">is turning '.$ty.' on <br>'.date('M jS Y',strtotime($tdc)).'</font>';
                //return 'is turning '.$ty.' on '.$tdc;
            }
        }
        else//today
        {
            $ty=floor((strtotime($tdc)-strtotime($tdo))/(3600*24*365));
            if($ty>0)
            {
                $td='today';
                $ty='<font color="#C7C5C5">has turned <br>'.$ty.' on today </font>';
                //return 'has turned '.$ty.' on today';
            }
            else
            {
                $ty='<font color="#C7C5C5">today</font>';$td='';
                //return '';
            }
        }
    }
    else
    {
        $ty='';$td='';
        //return '';
    }
    $ta[0]=$ty;
    $ta[1]=$td  ;
    return $ta;
}

I use below code to show the days remaining:

while($rs=mysql_fetch_array($result))
{
if (isset($rs['byear'],$rs['bmonth'],$rs['bdate']))
  {
    $tmptxt = retage($rs['byear'],$rs['bmonth'],$rs['bdate']);
    echo $tmptxt[1];
  }
}

The strange thing is that for one user, the days remaining is shown correctly eg:

gone 120 days ago

And for other user having same birth date, this is shown:

Jan 1st 1970  -14755 days to go

Strange:

When I use the same function outside of the loop and test with date 1985-01-26, the correct result is shown.

Note: You can check out the function for yourself.

Could you please tell what could be wrong there, your help will be highly appreciated.

Thanks.

+3  A: 

Jan 1st 1970 is what you get with a unix timestamp of 0 / invalid date. Are you sure the data in the database is accurate?

CharlesLeaf
@CharlesLeaf: Yes data is perfectly correct and same for those two users.
Sarfraz
What if you force all numbers to be integers? There MUST be a difference. Try this, it'll break the script: print_r( retage( 1985, " 01", 26 ) );
CharlesLeaf
@CharlesLeaf: Great, it broke it, how to solve it now?
Web Logic
How about doing what I said, force all arguments to be integers? Just a little `$iy = intval($iy); $im = intval($im); $id = intval(id);` as the first 3 lines in the function. That forces all arguments to be an integer and therefor can't contain any other characters.
CharlesLeaf
@CharlesLeaf: It worked, thanks man :)
Sarfraz