0

i don't know where its going wrong, when the comment is inserted it isn't echoing properly, all it shows is 44years ago is it write to use date('Y-m-d H:i:s') for datetime type to insert comment

and how can i store data in utc format

here's my insertion code

 $conn = new PDO('mysql:host=localhost;dbname=db', 'root', '');
          $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          $query = "INSERT INTO comp_pt_comments(cat_id, post_id, commented_by, delete_flag, last_modified, comments)
            VALUES (?,?,?,'false',date('Y-m-d H:i:s'),?)";
          $stmt = $conn->prepare($query);
          $stmt->bindParam(1, $cat_id);
          $stmt->bindParam(2, $post_id);
          $stmt->bindParam(3, $commented_by);
          $stmt->bindParam(4, $comments);

          $stmt->execute(); 
          $count=$stmt->rowCount();
          $stmt->closeCursor();  

below is my echo

while( $row = mysql_fetch_array($comments_result) ){

        $temp=array("comment_id"=>$row['sno'],"commented_by"=>$row['commented_by'],"comments"=>$row['comments'],"cmnt_user_img"=>$row['image_path'],"posted_time"=>$row['last_modified'],"commented_user"=>$row['cmmnts_usr'],"reply_count"=>$row['reply_count']);
        $comments_array[$i]=$temp;
        $i++;

    }

echo time_elapsed_string($comments_array[$j]['posted_time']);

this is the function i have used

define("SECOND", 1);
define("MINUTE", 60 * SECOND);
define("HOUR", 60 * MINUTE);
define("DAY", 24 * HOUR);
define("MONTH", 30 * DAY);
function time_elapsed_string($time)
{   

$delta = time() - $time;

if ($delta < 1 * MINUTE)
{
    return $delta == 1 ? 'one second ago' : $delta . ' seconds ago';
}
if ($delta < 2 * MINUTE)
{
  return 'a minute ago';
}
if ($delta < 45 * MINUTE)
{
    return floor($delta / MINUTE) . ' minutes ago';
}
if ($delta < 90 * MINUTE)
{
  return 'an hour ago';
}
if ($delta < 24 * HOUR)
{
  return floor($delta / HOUR) . ' hours ago';
}
if ($delta < 48 * HOUR)
{
  return 'yesterday';
}
if ($delta < 30 * DAY)
{
    return floor($delta / DAY) . ' days ago';
}
if ($delta < 12 * MONTH)
{
  $months = floor($delta / DAY / 30);
  return $months <= 1 ? 'one month ago' : $months . ' months ago';
}
else
{
    $years = floor($delta / DAY / 365);
    return $years <= 1 ? 'one year ago' : $years . ' years ago';
}
} 
BenMorel
  • 34,448
  • 50
  • 182
  • 322
stacky
  • 311
  • 1
  • 6
  • 26
  • Your logic is flawed anyway. Not all months have 30 days. – Amal Murali Apr 09 '14 at 07:16
  • i have used that function from [here](http://stackoverflow.com/a/1248/1608900) – stacky Apr 09 '14 at 07:18
  • Can you post some example inputs and the expected output? – Amal Murali Apr 09 '14 at 07:30
  • If you get 44 years ago, that means you're probably getting `01-01-1970`, in other words; you're not getting the right date back. – Gerben Jacobs Apr 09 '14 at 07:37
  • expected output is to display time in x hrs ago or x days ago accordingly, code is about inserting a comment, and it isn't saving the actual time instead its saving in **0000-00-00 00-00-00** which funtion i need to use to insert the time, **date('Y-m-d H:i:s')** is this the write one – stacky Apr 09 '14 at 07:41
  • what kinda example inputs, sorry i couldn't understand?? – stacky Apr 09 '14 at 07:56
  • @GerbenJacobs yeah xactly, but i havn't used timestamp to store the data – stacky Apr 09 '14 at 08:02

0 Answers0