[ PHPXref.com ] [ Generated: Sun Jul 20 16:35:25 2008 ] [ bBlog 0.7.6 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/bblog/bBlog_plugins/ -> modifier.date_format.php (source)

   1  <?php
   2  // modifier.date_format.php - smarty modifier to format a timestamp
   3  function smarty_modifier_date_format($date, $format="%F %j, %Y, %g:%i %a") {
   4    if($date < 1 ) return '';
   5    // locale should be defined in a config file, not case by case
   6    define('C_LOCALE','en_GB');
   7    setlocale(LC_TIME,C_LOCALE);
   8    
   9    switch ($format) {
  10      case "full": return strftime("%A, %d. %B %Y, %H:%M", $date);
  11           break;
  12      
  13      case "date": return strftime("%A, %d. %B %Y", $date);
  14          break;
  15      
  16      case "europe": return strftime("%d.%m.%Y", $date);
  17          break;
  18      
  19      case "shortdate": return strftime("%x", $date);
  20          break;
  21      
  22      case "month": return strftime("%B", $date);
  23          break;
  24      
  25      case "year": return strftime("%Y", $date);
  26          break;
  27      
  28      case "monthyear": return strftime("%B %Y", $date);
  29          break;
  30      
  31      case "time": return strftime("%H:%M", $date);
  32          break;
  33      
  34      case "s1" : return date("F j, Y, g:i a",$date);
  35          break;
  36      
  37      case "s2" : return date("F j, Y",$date);
  38          break;
  39      
  40      case "atom" : return date('Y-m-d\TH:i:s\Z',$date);
  41          break;
  42      
  43      case "rss20" : return strftime("%a, %d %b %Y %H:%M:%S %Z", $date);
  44          break;
  45      
  46      case "rss92" : return strftime("%a, %d %b %Y %H:%M:%S %Z", $date);
  47          break;
  48      
  49      case "suffix" : return date("S", $date);
  50          break;
  51      
  52      // a clever little hack to make date() return a ISO 8601 standard date string for use in RSS 1.0
  53      case "rss10" : return substr(date("Y-m-d\Th:i:sO", $date),0,22).":".substr(date("O", $date),3);
  54           break;
  55      
  56      // called Jim in reference to RevJim ( revjim.net ) who first used this format ( afict )
  57      case "jim" : return since($date)." on ".date("F j, Y",$date); 
  58          break;
  59      
  60      case "since" : return since($date);
  61          break;
  62    
  63        default:
  64           //default should behave like the original smarty date_format
  65             //see if there is at least one % in the date. then we go for new format
  66            if (substr_count("$format", '%') > 0) {
  67                return strftime($format, $date);     
  68            }
  69          //else we go the old date() way for backward compatibility
  70            else{
  71                return date($format, $date);
  72            }
  73            break;
  74    }//switch
  75   
  76  }//function
  77  
  78  function identify_modifier_date_format () {
  79    return array (
  80      'name'           =>'date_format',
  81      'type'           =>'smarty_modifier',
  82      'nicename'       =>'Date Format',
  83      'description'    =>'Date format takes a timestamp, and turns it into a nice looking date',
  84      'authors'         =>'Dean Allen, Eaden McKee, Tobias Schlottke',
  85      'licence'         =>'Textpattern'
  86    );
  87  }
  88  
  89  function bblog_modifier_date_format_help () {
  90  ?>
  91  <p>Date format takes a timestamp, and turns it into a nice looking date.
  92  <br />It is used as a modifier inside a template. For example, if you are in a
  93   <span class="tag">{post} {/post}</span> loop, you will have the varible {$post.dateposted}
  94   set which will contain a timestamp of when the post was made,
  95   and you will apply the date_format modifier to this tag.</p>
  96  <p>Examples :<br />
  97  <span class="tag">{$post.dateposted|date_format}</span> will return a date like May 26, 2003, 2:29 pm<br />
  98  <span class="tag">{$post.dateposted|date_format:since}</span> will return Posted 7 hours, 3 minutes ago<br />
  99  <span class="tag">{$post.dateposted|date_format:"F j, Y"}</span> will return May 26, 2003. The "F j, Y" is in php date() format, for more infomation see <a href="http://www.php.net/date">php.net/date</a></p>
 100  
 101  
 102  <?php
 103  }
 104  
 105  function formatsince($sum1,$desc1,$sum2,$desc2){
 106      if($sum1 == 1 && $sum2 == 0){
 107          $diff = "1 $desc1";
 108      }elseif($sum1 == 1 && $sum2 == 1){
 109          $diff = "1 $desc1, 1 $desc2";
 110      }elseif($sum1 == 1 && $sum2 > 1){
 111          $diff = "1 $desc1, $sum2 {$desc2}s";    
 112      }elseif($sum1 >1 && $sum2 == 1){
 113          $diff = "$sum1 {$desc1}s, 1 $desc2";
 114      }elseif($sum1 > 1 && $sum2 > 1){
 115          $diff = "$sum1 {$desc1}s, $sum2 {$desc2}s";
 116      }else{
 117          return false;
 118      }
 119      
 120      return $diff;    
 121     
 122  }
 123          
 124  function since($tstamp){
 125      $seconds = time() - $tstamp;
 126  
 127      $minutes = intval($seconds/60);
 128      $seconds = $seconds % 60;
 129  
 130      $hours = intval($minutes/60);
 131      $minutes = $minutes % 60;
 132  
 133      $days = intval($hours/24);
 134      $hours = $hours % 24;
 135  
 136      $weeks = intval($days/7);
 137      $days = $days % 7;
 138      
 139      $months = intval($weeks/4);
 140      $weeks = $weeks % 4;
 141      
 142      $years = intval($months/12);
 143      $months = $months % 12;
 144      
 145      if($diff = formatsince($years,"year",$months,"month")){
 146      
 147      }elseif($diff = formatsince($months,"month",$days,"day")){
 148      
 149      }elseif($diff = formatsince($weeks,"week",$days,"day")){
 150      
 151      }elseif($diff = formatsince($days,"day",$hours,"hour")){
 152      
 153      }elseif($diff = formatsince($hours,"hour",$minutes,"minute")){
 154      
 155      }elseif($diff = formatsince($minutes,"minute",$seconds,"second")){
 156          
 157      }else{
 158          $diff = "some seconds";
 159      }   
 160      return "Posted ".$diff. " ago";
 161  }
 162          
 163  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]