Jump to content
php.lv forumi

Facebook, twitter stila posta pievienosanas datums


vitao.web

Recommended Posts

Labdien!

 

Paldies foruma biedriem, kas ļāva dabūt gatavu šo te "scriptu". Gan jau ka daudziem jau tāds ir , bet ir arī tādi, kā es piemēram, kas vēl daudz ko nezina, tādēļ šis būtu noderīgs.

 

Pati funkcija:

function getAge($iTimestamp) {

   $iCurrentTimestamp = time();   

   $iDifference = $iCurrentTimestamp - $iTimestamp;

   $iOriginalDifference = $iDifference;
   $periods = array("second", "minute", "hour", "day",
                         "week", "month", "year", "decade");
   $lengths = array("60","60","24","7","4.35","12","10");

   for($j = 0; $iDifference >= $lengths[$j]; $j++)
       $iDifference /= $lengths[$j];

   if ($j == 6) {
       $sReturn = date('M Y',time() - $iOriginalDifference);
   } else {
       $iDifference = round($iDifference);
       if($iDifference != 1) $periods[$j] .= "s";
       $sReturn = $iDifference . " " . $periods[$j] . " " .
                      $ending . " ago";
   }

   if (trim($sReturn) == '1 day  ago') $sReturn = "yesterday";

   return $sReturn;

}

 

 

Un pielietojums, kādu nu katrs grib, te piemēri daži:

$age = getAge(time() - 3600);

 

vai ja izmanto DB:

$izs = dbFetch("SELECT id, title, cat,message,DATE_FORMAT(date, '%b %d %Y %h:%i %p') AS newdate, UNIX_TIMESTAMP(date) AS laiks
               FROM blog 
               ORDER BY id 
               DESC LIMIT 8;");

   while($rez = mysql_fetch_assoc($izs)){
        $time = $rez['laiks'];
        $age = getAge($time);
//talak jau to ko tev vajag.
}

 

 

Ceru, ka kādam noderēs!

Un vēlreiz paldies palīgiem ;)

Edited by vitao.web
Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...