Passed
Branch master (7dd754)
by Jean-Christophe
16:28
created

UDateTime   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 88.37%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 40
dl 0
loc 80
ccs 38
cts 43
cp 0.8837
rs 10
c 1
b 0
f 0

9 Methods

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