Jump to content

[SOLVED] dynamic date tracker for blog posts


Lambneck

Recommended Posts

Hello, I was wondering does anyone knows how to create something to track the time/date of a blog post?

But a dynamic one that works like the one used on twitter where it shows recent posts as something like "2 minutes ago" or "6 hours ago", and then shows older posts as the time and date ex. "12:00 PM May 25th".

 

 

Link to comment
Share on other sites

The time of the post is recorded in the database. The date calculation uses this field to display how long ago the post was created. Easy. You would use a database query to obtain the most recent posts if you liked using the date field.

Link to comment
Share on other sites

Ok, but i want the way the time is displayed to change based on how old the post is. Within 24 hours it would display ex. "6 hours ago". After twenty four hours it would display ex. "1:30 PM May 20th".

Im not sure if this is a php question, if it can be done with PHP or not.

Link to comment
Share on other sites

I know how to record the time/date to database and then display it, what Im trying to do here is make it so the way it is displayed changes depending upon whether it is a recent post or not. recent posts being displayed as "...hours ago" and then older posts display simply as their dates they were posted. how to have the time display as "...hours ago" change to date after 24 hours?

Link to comment
Share on other sites

ugh, I'm not sure I understand.

Should it look anything like the following?

What to echo when it is "//less than 24 hours"?

 

<?php

$postedtime = time()

$time_difference = time() - $postedtime;

if($time_difference < (60*60*24)) {
//less than 24 hours
} else {
echo date("M dS, Y", $row['date']); //more than 24 hours
}

 

Link to comment
Share on other sites

You should just have a play, its not hard to work out, here's a start;

 

<?php
echo ceil($time_difference/60); //prints the number of seconds rounded up
echo ceil($time_difference/3600); //prints the number of minutes rounded up

Link to comment
Share on other sites

do i need to put more "if" or "else" statements inbetween them or just like the following?

 

<?php

$postedtime = time()

$time_difference = time() - $postedtime;

if($time_difference < (60*60*24)) {
//less than 24 hours
echo (ceil($time_difference/60) . "seconds ago"); //prints the number of SECONDS rounded up
echo (ceil($time_difference/3600) . "minutes ago"); //prints the number of MINUTES rounded up
echo (ceil($time_difference/21600) . "hours ago"); //prints the number of HOURS rounded up
} else {
echo date("M dS, Y", $row['date']); //more than 24 hours
}

Link to comment
Share on other sites

That depends on what you want to print out, I do wish you would at least attempt some of this yourself, see neil's last post

 

http://www.phpfreaks.com/forums/index.php/topic,253898.msg1193148.html#msg1193148

 

Just for your information I messed up the commenting

 

echo (ceil($time_difference) . " seconds ago"); //prints the number of SECONDS rounded up
echo (ceil($time_difference/60) . " minutes ago"); //prints the number of MINUTES rounded up
echo (ceil($time_difference/1440) . " hours ago"); //prints the number of HOURS rounded up

Link to comment
Share on other sites

Sorry, Im trying, but not getting it yet.

what about this?

 

<?php
function postTime($time) 
{
if($time_difference < (60*60*24)) {
//less than 24 hours 
echo (ceil($time_difference) . " seconds ago"); //prints the number of SECONDS rounded up
echo (ceil($time_difference/60) . " minutes ago"); //prints the number of MINUTES rounded up
echo (ceil($time_difference/1440) . " hours ago"); //prints the number of HOURS rounded up
} else {
echo date("M dS, Y", $row['date']); //more than 24 hours
}
return $time;
}

 

 

Link to comment
Share on other sites

No, miles off and those functions wont work!

 

Use this example and try to learn the date & time functions from the php manual

<?php
function timeDiff($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;
		}
	}
}
return($diff);
}

$postTime  = strtotime("-2 hours");
$diffArray = timeDiff($postTime);

print "<pre>";
print_r($diffArray);
print "</pre>";
?>

Link to comment
Share on other sites

Still not getting the amount of time to display  ???

 

using:

<?php
function timeDiff($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;
		}
	}
}
return($diff);
}

$postTime  = strtotime("-2 hours");
$diffArray = timeDiff($postTime);

print "<pre>";
print_r($diffArray);
print "</pre>";
?>

 

displays:

Array

(

    [years] => 0

    [months] => 0

    [weeks] => 0

    [days] => 0

    [hours] => 1

    [minutes] => 59

    [seconds] => 59

)

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.