Long time ago…

This function is interesting for put in to a typical news column for indicate the time ago that the news are published o for show the time spend between publish posts in to a forum.

The use is more easy, only you need call function indicating the data in format (YYYY-MM-DD) that is the default data format that return MySQL.

Other option is delete the lines 2,3,4, and 5 and indicate directly the data in format Unix.

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
function long_time_ago( $data_mysql ){
	$data_start = explode(" ", ($data_mysql));
	$day_start  = explode("-", $data_start[0]);
	$hour_start = explode(":", $data_start[1]);
	$data_unix  = mktime($hour_start[0], $hour_start[1], $hour_start[2], $day_start[1], $day_start[2], $day_start[0]);
	$ht         = time() - $data_unix;
 
	if( 2116800 <= $ht ){
                $day_end     = date('d', $data_unix);
                $month_end   = date('n', $data_unix);
                $year_end    = date('Y', $data_unix);
                $hour_end    = date('H', $data_unix);
                $minut_end   = date('i', $data_unix);
                $month_array = array('', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
                $data_end = "$day_end $month_array[$month_end] $year_end";
	}
	if( 60 > $ht ){
                $data_end = "$ht seconds ago";
	} elseif( 3570 > $ht ){
                $hc = round( $ht / 60 );
                if( 1 < $hc){
                        $s = "s";
                }
                $data_end = "$hc minute".$s." ago";
	} elseif( 84600 > $ht ){
                $hc = round( $ht / 3600 );
                if( 1 < $hc ){
                        $s = "s";
                }
                $data_end = "$hc hour".$s." ago";
                if( 4200 < $ht && 5400 > $ht ){
                        $data_end = "more than a hour ago";
                }
	} elseif( 561600 > $ht ){
                $hc = round( $ht / 86400 );
                if( 1 == $hc ){
                        $data_end = "yesterday";
                }
                if( 1 < $hc )
                        $data_end = "$hc days ago";
	} elseif ( 2116800 > $ht ){
                $hc = round( $ht / 604800 );
                if( 1 < $hc ){
                        $s = "s";
                }
                $data_end = "$hc week".$s." ago";
	} elseif( 30242054.045 > $ht ){
                $hc= round( $ht / 2629743.83 );
                if( 1< $hc ){
                        $s="es";
                }
                $data_end = "$hc month".$s." ago";
	}
	return $data_end;
}

Leave a Reply

Your email address will not be published. Required fields are marked *