ほとんどすべてのプロジェクトで、私はサイトを目を楽しませ、知覚をより快適にする一連の機能を使用しています。 私はこれらの機能を異なる場所で部分的に借りて、自分で部分的に書いた。 確かに、多くの人が同様のものを使用しますが、とにかく共有することにしました-おそらく彼らは誰かに役立つでしょう。
数値を基に名詞を活用したり、通常のロシアの月名で日付を表示したり、人間が読める形式で日付を表示したりできます(昨日、前日、2日3時間2分前、1年2か月後など)。
次のようなものを使用できます。
<acrnonym title=" <?php print r_date ( $timestamp , 'j M Y H:i' , false ); ?> "> <?php print human_date ( $timestamp , 2 , false ); ?> </acronym>
<acrnonym title=" <?php print r_date ( $timestamp , 'j M Y H:i' ); ?> "> <?php print human_date ( $timestamp ); ?> </acronym>
これは次のようなものを返します:
<acrnonym title="2 2009 23:39">1 </acronym>
<acrnonym title="2 23:39"></acronym>
(Habrは<acronym>タグを切り捨てます)
そしてこのように:
<?php
$count = 10 ;
printf ( '%d %s' , $count , declension ( $count , array( '' , '' , '' )));
?>
これは戻ります:
10
実際には、機能:
<?php
/**
*
*
* @var integer ,
* @var array
* @return string
*
* :
* $count = 10;
* sprintf('%d %s', $count, declension($count, array('', '', '')));
*
* :
* 10
*/
function declension ( $number , $words ) {
$number = abs ( $number );
if ( $number > 20 ) $number %= 10 ;
if ( $number == 1 ) return $words [0];
if ( $number >= 2 && $number <= 4 ) return $words [ 1 ];
return $words [ 2 ];
}
/**
*
*
* date(),
* F ( ),
* M —
*
* @var integer Unix-timestamp
* @var string date() F M
* @var boolean ,
* @return string
*/
function r_date ( $time = '' , $format = 'j M Y' , $cut_year = true ) {
if(empty( $time )) $time = time ();
if( $cut_year && date ( 'Y' ) == date ( 'Y' , $time )) $format = preg_replace ( '/o|y|Y/' , '' , $format );
$month = abs ( date ( 'n' , $time )- 1 );
$rus = array( '' , '' , '' , '' , '' , '' , '' , '' , '' , '' , '' , '' );
$rus2 = array( '' , '' , '' , '' , '' , '' , '' , '' , '' , '' , '' , '' );
$format = preg_replace (array( "'M'" , "'F'" ), array( $rus [ $month ], $rus2 [ $month ]), $format );
return date ( $format , $time );
}
/**
* (, "2 13 ")
*
* declension()
*
* @var integer Unix-timestamp
* @var integer
* @var boolean (, , )
* @var string F M, r_date()
* @return string
*/
function human_date ( $timestamp , $granularity = 1 , $use_terms = true , $default_format = 'j M Y' ) {
$curtime = time ();
$original = $timestamp ;
$output = '' ;
if( $curtime >= $original ) {
$timestamp = abs ( $curtime - $original );
$tense = 'past' ;
} else {
$timestamp = abs ( $original - $curtime );
$tense = 'future' ;
}
$units = array( 'years' => 31536000 ,
'weeks' => 604800 ,
'days' => 86400 ,
'hours' => 3600 ,
'min' => 60 ,
'sec' => 1 );
$titles = array( 'years' => array( '' , '' , '' ),
'weeks' => array( '' , '' , '' ),
'days' => array( '' , '' , '' ),
'hours' => array( '' , '' , '' ),
'min' => array( '' , '' , '' ),
'sec' => array( '' , '' , '' ));
foreach ( $units as $key => $value ) {
if ( $timestamp >= $value ) {
$number = floor ( $timestamp / $value );
$output .= ( $output ? ( $granularity == 1 ? ' ' : ' ' ) : '' ) . $number . ' ' . declension ( $number , $titles [ $key ]);
$timestamp %= $value ;
$granularity --;
}
}
if( $tense == 'future' ) {
$output = ' ' . $output ;
} else {
$output .= ' ' ;
}
if( $use_terms ) {
$terms = array( ' 1 ' => '' ,
'1 ' => '' ,
'2 ' => ''
);
if(isset( $terms [ $output ])) $output = $terms [ $output ];
}
return $output ? $output : ( function_exists ( 'r_date' ) ? r_date ( $original , $default_format ) : date ( $default_format , $original ));
}
?>
彼らが誰かのために役立つことを願っています。