Test Failed
Push — master ( 94e773...0148c5 )
by Jean-Christophe
06:30
created

UDateTime::elapsed()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 9.2222
c 0
b 0
f 0
cc 6
nc 16
nop 2
crap 6
1
<?php
2
3
namespace Ubiquity\utils\base;
4
5
class UDateTime {
6
	const MYSQL_DATETIME_FORMAT = 'Y-m-d H:i:s';
7
	const MYSQL_DATE_FORMAT = 'Y-m-d';
8
	private static $locales = [ "fr" => [ "fr","fr_FR","fr_FR.UTF-8" ],"en" => [ "en","en_US","en_US.UTF-8" ] ];
9
10
	private static function setLocale($locale) {
11 1
		if (isset ( self::$locales [$locale] )) {
12 1
			$locale = self::$locales [$locale];
13 1
		} else {
14
			$locale = self::$locales ["en"];
15
		}
16
		setlocale ( LC_TIME, $locale [0], $locale [1], $locale [2] );
17 1
	}
18 1
19
	public static function secondsToTime($seconds) {
20
		$hours = floor ( $seconds / 3600 );
21
		$mins = floor ( $seconds / 60 % 60 );
22
		$secs = floor ( $seconds % 60 );
23
		return sprintf ( '%02d:%02d:%02d', $hours, $mins, $secs );
24
	}
25
26
	public static function mysqlDate(\DateTime $date) {
27
		return $date->format ( self::MYSQL_DATE_FORMAT );
28
	}
29
30
	public static function mysqlDateTime(\DateTime $datetime) {
31
		return $datetime->format ( self::MYSQL_DATETIME_FORMAT );
32
	}
33
34
	public static function longDate($date, $locale = "en") {
35
		self::setLocale ( $locale );
36
		return utf8_encode ( strftime ( "%A %d %B %Y", strtotime ( $date ) ) );
37
	}
38
39
	public static function shortDate($date, $locale = "en") {
40
		self::setLocale ( $locale );
41
		return strftime ( "%AD", strtotime ( $date ) );
42
	}
43
44
	public static function shortDatetime($datetime, $locale = "en") {
45
		self::setLocale ( $locale );
46
		return strftime ( "%c", strtotime ( $datetime ) );
47
	}
48
49 1
	public static function longDatetime($datetime, $locale = "en") {
50 1
		self::setLocale ( $locale );
51 1
		return utf8_encode ( strftime ( "%A %d %B %Y, %T", strtotime ( $datetime ) ) );
52
	}
53
54
	/**
55
	 *
56
	 * @param string $datetime
57
	 * @param boolean $full
58
	 * @return string
59
	 * @see http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago
60 1
	 */
61 1
	public static function elapsed($datetime, $full = false) {
62 1
		$now = new \DateTime ();
63 1
		$ago = new \DateTime ( $datetime );
64
		$diff = $now->diff ( $ago );
65 1
66 1
		$diff->w = floor ( $diff->d / 7 );
67
		$diff->d -= $diff->w * 7;
68
69 1
		$string = array ('y' => 'year','m' => 'month','w' => 'week','d' => 'day','h' => 'hour','i' => 'minute','s' => 'second' );
70
		foreach ( $string as $k => &$v ) {
71
			if ($diff->$k) {
72
				$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
73
			} else {
74
				unset ( $string [$k] );
75
			}
76
		}
77 1
78 1
		if (! $full)
79 1
			$string = array_slice ( $string, 0, 1 );
80
		return $string ? implode ( ', ', $string ) . ' ago' : 'just now';
81 1
	}
82
}
83
84