Time Left to Post

posted by Subzero on 2009-07-23 09:49:47
Hello, sorry for my bad english, I wish that when I post an article on wordpress, I bait him how long ago I posted, for example, "10 seconds ago," or "1 minutes ago" or "2 hours ago" or "22/07/2009" .
i have create this code:
<?php
	$giorno = "luglio 23, 2009"; //giorno
	$mesi_it = array("gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre");
	$mesi_en = array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
	$quando =  str_replace($mesi_it, $mesi_en, $giorno);
 
	if( $quando == date( "m d, Y", strtotime( "now" ) ) ){
 
		$ora = explode(":", "05:33"); //ora
		$stmp = date( "H", strtotime( "now" ) );
		if( $ora[0] == $stmp ){
			$minuti = explode(":", "05:33");
			$stmm = date( "i", strtotime( "now" ) );
			$da_quanto = ($minuti[0] - $stmm);
			echo str_replace("-", "", $da_quanto) . "minuti fa";
		} else {
			$da_quanto = ($ora[0] - $stmp);
			echo str_replace("-", "", $da_quanto) . " ore fa";
		}
 
	} else {
 
		//GIORNO
		$giorno = explode(",", $quando);
		$giorno_p = explode(" ", $giorno[0]);
		echo $giorno_p[1] . " ";
 
		//MESE
		$mese = explode(" ", $quando);
		echo substr_replace(str_replace($mesi_en, $mesi_it, $mese[0]), "", 4) . " ";
 
		//ANNO
		$anno = explode(",", $quando);
		$anno_p = explode(" ", $anno[1]);
		$count = 2;
		$anno_pp = substr($anno_p[1], (strlen($anno_p[1]) - $count), strlen($anno_p[1]));
		echo $anno_pp;
 
	}
?>
but it is not true, if you just put the article leaves me 17 minutes, even if I just posted. someone knows how to fix it?
dail avatar
dail
12
Hi Subzero, try it:
 
function post_diff($date) {
 
     if(empty($date)) return "The date is empty";     
 
     $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
     $lengths         = array("60","60","24","7","4.35","12","10");     
     $now             = time();
     $unix_date       = strtotime($date);
 
     // check validity of date
     if(empty($unix_date)) {    
         return "The date is invalid";
     }
 
     // is it future date or past date
     if($now > $unix_date) {    
         $difference = $now - $unix_date;
         $tense      = "ago";
 
     } else {
         $difference = $unix_date - $now;
         $tense      = "from now";
     }
 
     for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++){
         $difference /= $lengths[$j];
     }
 
     $difference = round($difference);
 
     if($difference != 1) {
         $periods[$j].= "s";
     }
 
     return "$difference $periods[$j] {$tense}";
 }
 
 
echo post_diff("2009-07-23 10:39:30"); // 3 minutes ago
echo post_diff("2009-07-23 10:43:30"); // 13 seconds from now
echo post_diff("2009-06-23 10:43:30"); // 4 weeks ago
 
Bye!
49 answers - 0 questions
  Positive        Negative

Subzero avatar
Subzero
0
I have a problem, because on wordpress to do what you have done that is:
echo post_diff("2009-07-23 10:39:30");
there are functions such as:
<?php the_time( 'Y-m-d' ); ?> <?php the_time( 'H:i:s' ); ?>
that even if you do not put echo in front, you also print that.
there is a solution to use these functions capturing these function? because if I do this:
$x =  the_time( 'H:i:s' ); 
this print O.o
Sorry for my bad English ;)
Thanks
7 answers - 3 questions
  Positive        Negative

dail avatar
dail
12
tell me, how the date/time are saved in the db. I think there is a wordpress function to "get" (not print) it.
49 answers - 0 questions
  Positive        Negative

Subzero avatar
Subzero
0
Database: my_socialgeeks Tabella: sg_posts Campo: post_date_gmt value: 2009-07-22 17:45:20
is true?
7 answers - 3 questions
  Positive        Negative

dail avatar
dail
12
Posted by Subzero:
Database: my_socialgeeks Tabella: sg_posts Campo: post_date_gmt value: 2009-07-22 17:45:20
is true?

good! you have to pass it to the post_diff function.
49 answers - 0 questions
  Positive        Negative

Subzero avatar
Subzero
0
Ok thanks, I had this thought myself, the only problem is that I do not know how you do, I understand the php, but mysql still nothing!
7 answers - 3 questions
  Positive        Negative

dail avatar
dail
12
Posted by Subzero:
Ok thanks, I had this thought myself, the only problem is that I do not know how you do, I understand the php, but mysql still nothing!

Post the script here... I will try to help you.
49 answers - 0 questions
  Positive        Negative

livin52 avatar
livin52
3
Try this:
 
the_date('Y-m-d H:i:s', '', '', FALSE);
 
6 answers - 0 questions
  Positive        Negative



Answer the question



Top Users
  • dail (12)
  • livin52 (3)
  • camu (3)
  • softweb (2)
  • Nadine (1)
  • Josware (1)
  • lfelipecr (1)
  • gregoriohc (1)
  • Mitu (1)
  • ryan714 (1)