-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
69 lines (59 loc) · 1.99 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
function cutText($text, $max = 300)
{
$array = explode(' ', $text);
$result = [];
$count = 0;
$readmore = '';
foreach ($array as $item) {
if ($count > $max) {
$readmore = '<a class="post-text__more-link" href="#">Читать далее</a>';
$result[] = '...';
break;
}
$count += strlen($item) + 1;
$result[] = $item;
}
return '<p>' . implode(' ', $result) . '</p>' . $readmore;
};
function esc($str)
{
$text = htmlspecialchars($str);
return $text;
};
function calcRelativeTime($ts)
{
$now = time();
$diff_sec = $now - $ts;
$minutes60 = 60 * 60;
$hours24 = 24 * 3600;
$days7 = 7 * 86400;
$weeeks5 = 5 * 604800;
$result = '';
if ($diff_sec < $minutes60) {
$minuts = ceil($diff_sec / 60);
$result = $minuts . ' ' . get_noun_plural_form($minuts, 'минута', 'минуты', 'минут');
} else if ($diff_sec >= $minutes60 && $diff_sec < $hours24) {
$hours = ceil($diff_sec / 3600);
$result = $hours . ' ' . get_noun_plural_form($hours, 'час', 'часа', 'часов');
} else if ($diff_sec >= $hours24 && $diff_sec < $days7) {
$days = ceil($diff_sec / 86400);
$result = $days . ' ' . get_noun_plural_form($days, 'день', 'дня', 'дней');
} else if ($diff_sec >= $days7 && $diff_sec < $weeeks5) {
$weeks = ceil($diff_sec / 604800);
$result = $weeks . ' ' . get_noun_plural_form($weeks, 'неделя', 'недели', 'недель');
} else {
$months = ceil($diff_sec / 2629743);
$result = $months . ' ' . get_noun_plural_form($months, 'месяц', 'месяца', 'месяцев');
}
return $result . ' назад';
}
function select_query($con, $query)
{
$result = mysqli_query($con, $query);
if (!$result) {
$error = mysqli_error($con);
print("Ошибка MySQL: " . $error);
};
return mysqli_fetch_all($result, MYSQLI_ASSOC);
}