1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

php Twitter Like time

Discussion in 'PHP' started by jkewlo, Jul 8, 2009.

  1. #1
    so i am trying to get time like twitter/facebook/myspace my problem is that it is posting the time/date of the current time/date

    I have 2 funtions

    one being time_since() and add_post()

    time_since() Time since is the function that returns year, day, minutes, seconds since.
    
     function time_since($time){
    //The Output the time is used at
    $format = "F m, Y g:i a";
     
    //Time presets for the lazy (Time goes by seconds)
    $timeyear = 365 * 24 * 60 * 60;
    $timemonth = 30 * 7 * 24 * 60 * 60;
    $timeweek = 7 * 24 * 60 * 60;
    $timeday = 24 * 60 * 60;
    $timehour = 60 * 60;
    $timemins = 60;
    $timeseconds = 1;
     
    //today's date
    $today = time();
     
    //Get the time from today by minusing the time looked at by today's date
    $x = $today - $time;
     
    //These define the out put
    if($x >= $timeyear){$x = date($format, $x); $dformat=""; $pre ="On the date: "; 
    }elseif($x >= $timemonth){$x = date($format, $x); $dformat=""; $pre ="On the date: ";
    }elseif($x >= $timeday){$x = round($x / $timeday); $dformat="days ago"; $pre ="About"; $x = round($x);
    }elseif($x >= $timehour){$x = round($x / $timehour); $dformat="hours ago"; $pre ="About"; 
    }elseif($x >= $timemins){$x = round($x / $timemins); $dformat="minutes ago"; $pre ="About";
    }elseif($x >= $timeseconds){$x = round($x / $timeseconds); $dformat="seconds ago"; $pre ="About"; 
    }
    return $pre." ".$x." ".$dformat;
    }
    
    Code (markup):
    add_post() adds the post to the database.
    
      function add_post($userid, $body)
      {
       $body = str_replace(":)","<img src=\"img/smilehappy.png\">",$body);
       $body = str_replace(":(","<img src=\"img/smilesad.png\">",$body);
       $body = str_replace("X(","<img src=\"img/smilemad.png\">",$body);
       $body = str_replace("o.O","<img src=\"img/smileoO.png\">",$body);
       $body = str_replace("><","<img src=\"img/smilesquint.png\">",$body);
       $body = str_replace(">_<","<img src=\"img/smileninja.png\">",$body);
       $body = str_replace(":S","<img src=\"img/smilesick.png\">",$body);
       $body = str_replace("zZz","<img src=\"img/smileZz.png\">",$body);
       $body = str_replace("oO","<img src=\"img/smilepo.png\">",$body);
       
          $sql = "insert into posts (user_id, body, stamp) 
          values ($userid, '" . mysql_real_escape_string($body) . "', now())";
          
          $result = mysql_query($sql);
      }
    
    Code (markup):
    then profile.php I have called the function time_since()

    
     <?php
    $dateofpost = time_since($time);
    $posts = show_posts($_SESSION['userid'], $list['id']);
    
    if (count($posts)){
    ?>
      <table border='0' cellspacing='0' cellpadding='0' >
      <?php
    foreach ($posts as $key => $list){
    	echo "<tr valign='top'>\n";
    	echo "<td><a href=\"/". $list['username'] ."\"><font color=black>".$list['username'] ."</font></a></td>\n";
    	echo "<td>&nbsp;<font color=black><strong>".$list['body'] ."</strong></font> <a href=\"remove.php?id=". $list['id'] ."\">Remove</a><br/>\n";
    	echo "<small>&nbsp;<font color=black>". $dateofpost ."</font></small></td>\n";
    	echo "</tr>\n";
    }
    ?>
      </table>
      <?php
    }else{
    ?>
      <p><b>You haven't posted anything yet!</b></p>
      <?php
    }
    ?>
    
    Code (markup):
    but it returns
    Grind3r | what are you thinking?
    On the date: July 07, 2009 7:42 pm

    and that updates when the time changes and I posted that yesterday at like 6:00 pm.

    anything anyone can do and or point me in the right direction
     
    jkewlo, Jul 8, 2009 IP