Timestamp

Standard

I decided that I would try and add a timestamp to the post that come up on the homepage. To do this I needed to add a timestamp row on PHPMyAdmin so it would record the time that the post was submitted. Adding this feature would prove to be  an aesthetic element to the posts and also meant that sorting the post by most recent would be easier to do.

This is a video someone recommened me to watch that gave me the details to add a timestanmp onto the post:

I used this video to understand what code to write and how it worked. I ended up changing the layout of the timestamp so that It said “3 hours ago” or “2 minutes ago”.

Firstly I had to create the function which would cause the time to be posted in the post box. This code also included the way that the timestamp would be printed and named the function as “$timestamp”. The code below is the code I wrote for the functions.php file.

class convertToAgo {
    function convert_datetime($str) {
        //sets the timezone to UTC
	   date_default_timezone_set('UTC');
        //explode breaks a string into an array
   		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;
        //this function converts into a timestamp in the "2015-01-01 12:12:12" format
    }
    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($x = 0; $difference >= $lengths[$x]; $x++){
   			$difference /= $lengths[$x];
   			$difference = round($difference);
        }
   		if($difference != 1) $periods[$x].= "s";
   			$text = "$difference $periods[$x] ago";
   			return $text;
        //this function gives out the differences between the time and the timestamp-time
    }
} 

Screen Shot 2015-04-17 at 18.08.58

The next step was to place the function in box1.php as the timestamp needed to be shown in the box with the other profile information. This linked the database time data with the function so it could find the right information to use.

include_once("../includes/functions.php"); // Include the class library
    $timeAgoObject = new convertToAgo; // Create an object for the time conversion functions
    // Query your database here and get timestamp
    $ts = $row["dateofpost"];
    $convertedTime = ($timeAgoObject -> convert_datetime($ts)); // Convert Date Time
    $when = ($timeAgoObject -> makeAgo($convertedTime)); // Then convert to ago time
    ?>
    
    <div class="time_ago" <h2><?php echo "".$when; ?></h2> </div>

This is the code I placed into the box1.php file. Screen Shot 2015-04-17 at 18.08.01

I then styled this timestamp in the CSS by making the font slightly opaque and giving the text a different colour.

Screen Shot 2015-04-17 at 18.19.46

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s