Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
30d84ffb
Commit
30d84ffb
authored
Mar 19, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2714 from drenty/master
Added asElapsedTime function to Formatter
parents
5ae9b82e
ed70735b
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
90 additions
and
0 deletions
+90
-0
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
Formatter.php
framework/base/Formatter.php
+89
-0
FormatterTest.php
tests/unit/framework/base/FormatterTest.php
+0
-0
No files found.
framework/CHANGELOG.md
View file @
30d84ffb
...
...
@@ -145,6 +145,7 @@ Yii Framework 2 Change Log
-
Enh #2646: Added support for specifying hostinfo in the pattern of a URL rule (qiangxue)
-
Enh #2661: Added boolean column type support for SQLite (qiangxue)
-
Enh #2670: Changed
`console\Controller::globalOptions()`
to
`options($actionId)`
to (make it possible to) differentiate options per action (hqx)
-
Enh #2714: Added support for formatting time intervals relative to the current time with
`yii\base\Formatter`
(drenty)
-
Enh #2729: Added
`FilterValidator::skipOnArray`
so that filters like
`trim`
will not fail for array inputs (qiangxue)
-
Enh #2735: Added support for
`DateTimeInterface`
in
`Formatter`
(ivokund)
-
Enh #2756: Added support for injecting custom
`isEmpty`
check for all validators (qiangxue)
...
...
framework/base/Formatter.php
View file @
30d84ffb
...
...
@@ -470,4 +470,93 @@ class Formatter extends Component
return
$verbose
?
Yii
::
t
(
'yii'
,
'{n, plural, =1{# petabyte} other{# petabytes}}'
,
$params
)
:
Yii
::
t
(
'yii'
,
'{n} PB'
,
$params
);
}
}
/**
* Formats the value as the time interval between a date and now in human readable form.
* @param integer|string|DateTime|DateInterval $value the value to be formatted. The following
* types of value are supported:
*
* - an integer representing a UNIX timestamp
* - a string that can be parsed into a UNIX timestamp via `strtotime()` or that can be passed to a DateInterval constructor.
* - a PHP DateTime object
* - a PHP DateInterval object (a positive time interval will refer to the past, a negative one to the future)
*
* @return string the formatted result
*/
public
function
asRelativeTime
(
$value
,
$referenceTime
=
null
)
{
if
(
$value
===
null
)
{
return
$this
->
nullDisplay
;
}
if
(
$value
instanceof
\DateInterval
)
{
$interval
=
$value
;
}
else
{
$timestamp
=
$this
->
normalizeDatetimeValue
(
$value
);
if
(
$timestamp
===
false
)
{
// $value is not a valid date/time value, so we try
// to create a DateInterval with it
try
{
$interval
=
new
\DateInterval
(
$value
);
}
catch
(
Exception
$e
)
{
// invalid date/time and invalid interval
return
$this
->
nullDisplay
;
}
}
else
{
$timezone
=
new
\DateTimeZone
(
$this
->
timeZone
);
if
(
$referenceTime
===
null
)
{
$dateNow
=
new
DateTime
(
'now'
,
$timezone
);
}
else
{
$referenceTime
=
$this
->
normalizeDatetimeValue
(
$referenceTime
);
$dateNow
=
new
DateTime
(
null
,
$timezone
);
$dateNow
->
setTimestamp
(
$referenceTime
);
}
$dateThen
=
new
DateTime
(
null
,
$timezone
);
$dateThen
->
setTimestamp
(
$timestamp
);
$interval
=
$dateThen
->
diff
(
$dateNow
);
}
}
if
(
$interval
->
invert
)
{
if
(
$interval
->
y
>=
1
)
{
return
Yii
::
t
(
'yii'
,
'in {delta, plural, =1{a year} other{# years}}'
,
[
'delta'
=>
$interval
->
y
]);
}
if
(
$interval
->
m
>=
1
)
{
return
Yii
::
t
(
'yii'
,
'in {delta, plural, =1{a month} other{# months}}'
,
[
'delta'
=>
$interval
->
m
]);
}
if
(
$interval
->
d
>=
1
)
{
return
Yii
::
t
(
'yii'
,
'in {delta, plural, =1{a day} other{# days}}'
,
[
'delta'
=>
$interval
->
d
]);
}
if
(
$interval
->
h
>=
1
)
{
return
Yii
::
t
(
'yii'
,
'in {delta, plural, =1{an hour} other{# hours}}'
,
[
'delta'
=>
$interval
->
h
]);
}
if
(
$interval
->
i
>=
1
)
{
return
Yii
::
t
(
'yii'
,
'in {delta, plural, =1{a minute} other{# minutes}}'
,
[
'delta'
=>
$interval
->
i
]);
}
return
Yii
::
t
(
'yii'
,
'in {delta, plural, =1{a second} other{# seconds}}'
,
[
'delta'
=>
$interval
->
s
]);
}
else
{
if
(
$interval
->
y
>=
1
)
{
return
Yii
::
t
(
'yii'
,
'{delta, plural, =1{a year} other{# years}} ago'
,
[
'delta'
=>
$interval
->
y
]);
}
if
(
$interval
->
m
>=
1
)
{
return
Yii
::
t
(
'yii'
,
'{delta, plural, =1{a month} other{# months}} ago'
,
[
'delta'
=>
$interval
->
m
]);
}
if
(
$interval
->
d
>=
1
)
{
return
Yii
::
t
(
'yii'
,
'{delta, plural, =1{a day} other{# days}} ago'
,
[
'delta'
=>
$interval
->
d
]);
}
if
(
$interval
->
h
>=
1
)
{
return
Yii
::
t
(
'yii'
,
'{delta, plural, =1{an hour} other{# hours}} ago'
,
[
'delta'
=>
$interval
->
h
]);
}
if
(
$interval
->
i
>=
1
)
{
return
Yii
::
t
(
'yii'
,
'{delta, plural, =1{a minute} other{# minutes}} ago'
,
[
'delta'
=>
$interval
->
i
]);
}
return
Yii
::
t
(
'yii'
,
'{delta, plural, =1{a second} other{# seconds}} ago'
,
[
'delta'
=>
$interval
->
s
]);
}
}
}
tests/unit/framework/base/FormatterTest.php
View file @
30d84ffb
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment