Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2010
    Location
    Oslo, Norway
    Posts
    58
    Thanks
    10
    Thanked 1 Time in 1 Post

    Help with time ago

    hi, im trying to create something facbook like when you post a topic in the forum it shows for example 1 minute ago, but when i use this code it shows up -21244 secs ago, anyone who have an idea why it dosent show 1 second ago?


    time ago class called develop_php_libary.php
    PHP Code:
    <?php
    // CLASS FOR CONVERTING TIME TO AGO
    class convertToAgo {

        function 
    convert_datetime($str) {
        
               list(
    $date$time) = explode(' '$str);
            list(
    $year$month$day) = explode('-'$date);
            list(
    $hour$minute$second) = explode(':'$time);
            
    $timestamp mktime($hour$minute$second$month$day$year);
            return 
    $timestamp;
        }

        function 
    makeAgo($timestamp){
        
               
    $difference time() - $timestamp;
               
    $periods = array("sec""min""hr""day""week""month""year""decade");
               
    $lengths = array("60","60","24","7","4.35","12","10");
               for(
    $j 0$difference >= $lengths[$j]; $j++)
                   
    $difference /= $lengths[$j];
                   
    $difference round($difference);
               if(
    $difference != 1$periods[$j].= "s";
                   
    $text "$difference $periods[$j] ago";
                   return 
    $text;
        }
        
    // END CLASS
    ?>
    rightforum.php
    PHP Code:
    <?php
    session_start
    ();
    include_once 
    "includes/db_connect.php";
    include_once
    "includes/functions.php";
    include
    "includes/smile.php";
    include_once(
    "classes/develop_php_library.php"); // Include the class library


    $query=mysql_query("$query");
    $num=mysql_num_rows($query);
    while(
    $right=mysql_fetch_object($query)){


    if (
    $fetch->userlevel "0"){

    $timeAgoObject = new convertToAgo// Create an object for the time conversion functions
    // Query your database here and get timestamp
    $ts "$right->made";
    $convertedTime = ($timeAgoObject -> convert_datetime($ts)); // Convert Date Time
    $when = ($timeAgoObject -> makeAgo($convertedTime)); // Then convert to ago time

    $forumquote mysql_query("SELECT * FROM users WHERE username = '$right->username'");
    $fq mysql_fetch_object($forumquote);
    echo 
    "
    <table class='GridRow'>
    <tr class=GridHeader>
    <td>
    <a href='profile.php?viewuser=$right->username'>$right->username</a>
    [$when]
    <a href='rightforum.php?clean=$right->id&forum=$forum&viewtopic=$viewtopic'>[Delete]</a>
    </td>

    <tr><td width=433>"
    ;

    echo 
    replace(stripslashes($right->text));

    if(
    $fq->forumquote == "") {
    echo 
    "</td></tr></table><br>
    <p>"
    ;
    }
    else {
    echo 
    "</td></tr><tr></tr></table><br>
    <p>"
    ; }
    }elseif (
    $userlevel == 0){

    $timeAgoObject = new convertToAgo// Create an object for the time conversion functions
    // Query your database here and get timestamp
    $ts "$right->made";
    $convertedTime = ($timeAgoObject -> convert_datetime($ts)); // Convert Date Time
    $when = ($timeAgoObject -> makeAgo($convertedTime)); // Then convert to ago time


    $forumquote mysql_query("SELECT * FROM users WHERE username = '$right->username'");
    $fq mysql_fetch_object($forumquote);
    echo 
    "
    <table Class='GridRow'>
    <tr Class='GridHeader'>
    <td>

    <a href='profile.php?viewuser=$right->username' target=middle>$right->username</a>
    [$when]
    </td>
    </tr>
    <tr>
    <td width=433>
    </b>"
    ;

    echo 
    replace(stripslashes($right->text));


    if(
    $fq->forumquote == "") {
    echo 
    "</td></tr></table><br><p>";
    }
    else {
    echo 
    "</td></tr></table><br>
    <p>"
    ; }
    }



    }
    ?>
    yeah, i know it is a bit messy, im new to php

    Thanks in advance
    Last edited by Greenbox; 06-17-2010 at 02:47 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Looks ok to me, so it must be the data.
    Except for this: for($j = 0; $difference >= $lengths[$j]; $j++). The highest you can go is a decade, what happens if its greater than this? Chances are it won't be, but you should change this:
    PHP Code:
    $iLength count($periods); // or lengths, it doesn't matter since both should be the same length
    for($j 0$difference >= $lengths[$j] && $j $iLength$j++) 
    So, lets see what's in timestamp:
    PHP Code:
        function makeAgo($timestamp){
        
               
    $difference time() - $timestamp;
               
    printf("\$timestamp: %d, \$difference: %d\n"$timestamp$difference);
               
    $periods = array("sec""min""hr""day""week""month""year""decade");
               
    $lengths = array("60","60","24","7","4.35","12","10");
               for(
    $j 0$difference >= $lengths[$j]; $j++)
                   
    $difference /= $lengths[$j];
                   
    $difference round($difference);
               if(
    $difference != 1$periods[$j].= "s";
                   
    $text "$difference $periods[$j] ago";
                   return 
    $text;
        } 
    What does that show for your input data.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Mar 2010
    Location
    Oslo, Norway
    Posts
    58
    Thanks
    10
    Thanked 1 Time in 1 Post
    it shows me 6 hours when i post :P

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by Greenbox View Post
    it shows me 6 hours when i post :P
    So its working properly for you now?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New Coder
    Join Date
    Mar 2010
    Location
    Oslo, Norway
    Posts
    58
    Thanks
    10
    Thanked 1 Time in 1 Post
    it dosent show, -232342 but it shows 6 hours when i post it, it should say 1 second ago :P

  • #6
    New Coder
    Join Date
    Jan 2010
    Location
    UT
    Posts
    35
    Thanks
    1
    Thanked 3 Times in 3 Posts
    You can do this with a jQuery plugin. I use it myself actually.

    http://timeago.yarp.com/

    Its very easy to implement.
    Just here to help.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by Greenbox View Post
    it dosent show, -232342 but it shows 6 hours when i post it, it should say 1 second ago :P
    What does the actual time say for it? Being an even hour, I'm leading towards a timezone offset.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    New Coder
    Join Date
    Mar 2010
    Location
    Oslo, Norway
    Posts
    58
    Thanks
    10
    Thanked 1 Time in 1 Post
    The actual time:
    2010-06-17 09:21:16 -> 21 hrs ago

    im not sure if thats what you ment, but it shows 6 hours to much for some reason, it might be the timezone..
    Last edited by Greenbox; 06-18-2010 at 03:04 PM.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    If you create a brand new one, and check the time for it, does it list the proper time for it or does it show it as -6 hours instead of the 0-whatever seconds ago?
    If it does, its a timezone issue. This can be corrected by setting the proper timezone for what you would like to use.
    I'm fairly certain as well that using strtotime will parse both parts (date and time sections) into a proper timestamp. Since your format matches a valid input format (ISO-8601 / yyyy-mm-dd), this can be used in place of the convert_datetime method if you like.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #10
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Heres what I created for one of my recent systems.
    To use this function you just write this postTime('13-06-2010 03:19AM');
    PHP Code:
    <?php
    function postTime($time1)
    {
      
    $time1 strtotime($time1);
      
    $time2 time();
      
    $diff = array(
        
    'years' => 0'months' => 0'weeks' => 0'days' => 0'hours' => 0'minutes' => 0'seconds' => 0
      
    ); 
      foreach(array(
    'years','months','weeks','days','hours','minutes','seconds') as $unit)
      {
        while(
    true)
        {
          
    $next strtotime("+1 $unit"$time1);
          if(
    $next $time2)
          {
            
    $time1 $next;
            
    $diff[$unit]++;
          }
          else
          {
            break;
          }
        }
      }
      if(
    $diff['years'] > 0)
      {
        if(
    $diff['months'] > || $diff['weeks'] > || $diff['days'] > || 
           
    $diff['hours'] > || $diff['minutes'] > || $diff['seconds'] > 0)
        {
          
    $time = ($diff['years'] == '1')?"over {$diff['years']} year":"over {$diff['years']} years";
        }
        else
        {
          
    $time = ($diff['years'] == '1')?"{$diff['years']} year":"{$diff['years']} years";
        }
      }
      elseif(
    $diff['months'] > 0)
      {
        if(
    $diff['weeks'] > || $diff['days'] > || $diff['hours'] > || 
           
    $diff['minutes'] > || $diff['seconds'] > 0)
        {
          
    $time = ($diff['months'] == '1')?"over {$diff['months']} month":"over {$diff['months']} months";
        }
        else
        {
          
    $time = ($diff['months'] == '1')?"{$diff['months']} month":"{$diff['months']} months";
        }
      }
      elseif(
    $diff['weeks'] > 0)
      {
        if(
    $diff['days'] > || $diff['hours'] > || $diff['minutes'] > || $diff['seconds'] > 0)
        {
          
    $time = ($diff['weeks'] == '1')?"over {$diff['weeks']} week":"over {$diff['weeks']} weeks";
        }
        else
        {
          
    $time = ($diff['weeks'] == '1')?"{$diff['weeks']} week":"{$diff['weeks']} weeks";
        }
      }
      elseif(
    $diff['days'] > 0)
      {
        if(
    $diff['hours'] > || $diff['minutes'] > || $diff['seconds'] > 0)
        {
          
    $time = ($diff['days'] == '1')?"over {$diff['days']} day":"over {$diff['days']} days";
        }
        else
        {
          
    $time = ($diff['days'] == '1')?"{$diff['days']} day":"{$diff['days']} days";
        }
      }
      elseif(
    $diff['hours'] > 0)
      {
        if(
    $diff['minutes'] > || $diff['seconds'] > 0)
        {
          
    $time = ($diff['hours'] == '1')?"over {$diff['hours']} hour":"over {$diff['hours']} hours";
        }
        else
        {
          
    $time = ($diff['hours'] == '1')?"{$diff['hours']} hour":"{$diff['hours']} hours";
        }
      }
      elseif(
    $diff['minutes'] > 0)
      {
        if(
    $diff['seconds'] > 0)
        {
          
    $time = ($diff['minutes'] == '1')?"over {$diff['minutes']} minute":"over {$diff['minutes']} minutes";
        }
        else
        {
          
    $time = ($diff['minutes'] == '1')?"{$diff['minutes']} minute":"{$diff['minutes']} minutes";
        }
      }
      elseif(
    $diff['seconds'] > 0)
      {
        
    $time = ($diff['seconds'] == '1')?"{$diff['seconds']} second":"{$diff['seconds']} seconds";
      }
      if(!empty(
    $time))
      {
        return 
    "{$time} ago";
      }
    }
    ?>
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •