📜 ⬆️ ⬇️

A set of PHP functions to create a user-friendly interface in Russian

In almost every project I use a set of functions that make the site a little more pleasant to the eye and more convenient for perception. I partially borrowed these functions in different places and partially wrote myself. Surely, many people use similar ones, but I decided to share it all the same - perhaps they will be useful to someone.

They are able to incline nouns on a numerical basis, display a date with normal Russian names of months and display the date in a readable human-readable form (yesterday, the day before yesterday, 2 days 3 hours and 2 minutes ago, after 1 year and 2 months, etc.).

You can use them like this:
<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>


This will return something like this:
<acrnonym title="2 2009 23:39">1 </acronym>
<acrnonym title="2 23:39"></acronym>

(Habr cuts off the <acronym> tag)
')
And so:
<?php
$count
= 10 ;
printf ( '%d %s' , $count , declension ( $count , array( '' , '' , '' )));
?>


This will return:
10

Actually, the functions:

<?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 ));
}
?>


I hope someone will come in handy.

Source: https://habr.com/ru/post/48335/


All Articles