PHP: Strange Date Problem
Posted
by Me-and-Coding
on Stack Overflow
See other posts from Stack Overflow
or by Me-and-Coding
Published on 2010-05-26T12:51:32Z
Indexed on
2010/05/26
13:21 UTC
Read the original article
Hit count: 340
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.
© Stack Overflow or respective owner