PHP - Преобразование времени (human,time)
Войти

PHP - Преобразование времени (human,time)

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
70
71
72
73
74
75
76
<?php 

define
'MINUTE_IN_SECONDS'60 ); 
define'HOUR_IN_SECONDS'60 MINUTE_IN_SECONDS ); 
define'DAY_IN_SECONDS'24 HOUR_IN_SECONDS );
define'WEEK_IN_SECONDS'DAY_IN_SECONDS );  
function 
human_time_diff$from$to '' ) {
    if ( empty( 
$to ) ) {
        
$to time();
    }
    echo 
$from.'<br/>';
    echo 
$diff = (int) abs$to $from ).'<br/>';

    if (
$diff HOUR_IN_SECONDS) {
        echo 
999;
        
$mins round$diff MINUTE_IN_SECONDS );
        if ( 
$mins <= )
            
$mins 1;
/* translators: Time difference between two dates,
 in minutes (min=minute). 1: Number of minutes 
        //echo 'dsf'.$since = sprintf( _n( '%s min', '%s mins', $mins ), $mins );
        */
    

    else {
        
        
//echo 'sdf';
    
}
    
    
/**
    elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
        $hours = round( $diff / HOUR_IN_SECONDS );
        if ( $hours <= 1 )
            $hours = 1;
        /* translators: Time difference between two dates, in hours. 1: Number of hours 
        $since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
    } elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
        $days = round( $diff / DAY_IN_SECONDS );
        if ( $days <= 1 )
            $days = 1;
        /* translators: Time difference between two dates, in days. 1: Number of days 
        $since = sprintf( _n( '%s day', '%s days', $days ), $days );
    } elseif ( $diff < MONTH_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
        $weeks = round( $diff / WEEK_IN_SECONDS );
        if ( $weeks <= 1 )
            $weeks = 1;
        /* translators: Time difference between two dates, in weeks. 1: Number of weeks 
        $since = sprintf( _n( '%s week', '%s weeks', $weeks ), $weeks );
    } elseif ( $diff < YEAR_IN_SECONDS && $diff >= MONTH_IN_SECONDS ) {
        $months = round( $diff / MONTH_IN_SECONDS );
        if ( $months <= 1 )
            $months = 1;
        /* translators: Time difference between two dates, in months. 1: Number of months 
        $since = sprintf(( '%s month', '%s months', $months ), $months );
    } elseif ( $diff >= YEAR_IN_SECONDS ) {
        $years = round( $diff / YEAR_IN_SECONDS );
        if ( $years <= 1 )
            $years = 1;
        /* translators: Time difference between two dates, in years. 1: Number of years 
        $since = sprintf(( '%s year', '%s years', $years ), $years );
    }

    
     * Filters the human readable difference between two timestamps.
     *
     * @since 4.0.0
     *
     * @param string $since The difference in human readable text.
     * @param int    $diff  The difference in seconds.
     * @param int    $from  Unix timestamp from which the difference begins.
     * @param int    $to    Unix timestamp to end the time difference.
     *
    return  $since;
}


?>
Теги:
php