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
f7871e24
Commit
f7871e24
authored
Mar 12, 2014
by
David Renty
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
asElapsedTime can now take DateInterval as parameter
parent
a74fa20d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
17 deletions
+27
-17
Formatter.php
framework/base/Formatter.php
+27
-17
No files found.
framework/base/Formatter.php
View file @
f7871e24
...
...
@@ -458,49 +458,59 @@ class Formatter extends Component
/**
* Formats the value as the time elapsed since then in human readable form.
* @param integer|string|DateTime $value the value to be formatted. The following
* @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()`
* - a PHP DateTime object
* - a PHP DateInterval object
*
* @return string the formatted result
*/
public
function
asElapsedTime
(
$value
)
{
if
(
$value
===
null
)
{
return
$this
->
nullDisplay
;
}
$value
=
$this
->
normalizeDatetimeValue
(
$value
);
if
(
$value
instanceof
\DateInterval
)
{
$interval
=
$value
;
}
else
{
if
(
$value
===
null
)
{
return
$this
->
nullDisplay
;
}
$value
=
$this
->
normalizeDatetimeValue
(
$value
);
$now
=
new
\DateTime
(
);
$date
=
new
DateTime
(
null
,
new
\DateTimeZone
(
$this
->
timeZone
));
$date
->
setTimestamp
(
$value
);
$dateNow
=
new
DateTime
(
'now'
,
new
\DateTimeZone
(
$this
->
timeZone
)
);
$dateThen
=
new
DateTime
(
null
,
new
\DateTimeZone
(
$this
->
timeZone
));
$dateThen
->
setTimestamp
(
$value
);
$interval
=
$now
->
diff
(
$date
);
$interval
=
$dateNow
->
diff
(
$dateThen
);
}
if
(
$interval
->
y
>=
1
)
{
$delta
=
$interval
->
y
;
return
Yii
::
t
(
'yii'
,
'{delta, plural, =1{a year} other{# years}} ago'
,
[
'delta'
=>
$delta
]);
}
elseif
(
$interval
->
m
>=
1
)
{
}
if
(
$interval
->
m
>=
1
)
{
$delta
=
$interval
->
m
;
return
Yii
::
t
(
'yii'
,
'{delta, plural, =1{a month} other{# months}} ago'
,
[
'delta'
=>
$delta
]);
}
elseif
(
$interval
->
d
>=
7
)
{
}
if
(
$interval
->
d
>=
7
)
{
$delta
=
floor
(
$interval
->
d
/
7
);
return
Yii
::
t
(
'yii'
,
'{delta, plural, =1{a week} other{# weeks}} ago'
,
[
'delta'
=>
$delta
]);
}
elseif
(
$interval
->
d
>=
1
)
{
}
if
(
$interval
->
d
>=
1
)
{
$delta
=
$interval
->
d
;
return
Yii
::
t
(
'yii'
,
'{delta, plural, =1{yesterday} other{# days ago}}'
,
[
'delta'
=>
$delta
]);
}
elseif
(
$interval
->
h
>=
1
)
{
}
if
(
$interval
->
h
>=
1
)
{
$delta
=
$interval
->
h
;
return
Yii
::
t
(
'yii'
,
'{delta, plural, =1{an hour} other{# hours}} ago'
,
[
'delta'
=>
$delta
]);
}
elseif
(
$interval
->
i
>=
1
)
{
}
if
(
$interval
->
i
>=
1
)
{
$delta
=
$interval
->
i
;
return
Yii
::
t
(
'yii'
,
'{delta, plural, =1{a minute} other{# minutes}} ago'
,
[
'delta'
=>
$delta
]);
}
else
{
$delta
=
$interval
->
s
;
return
Yii
::
t
(
'yii'
,
'{delta, plural, =1{a second} other{# seconds}} ago'
,
[
'delta'
=>
$delta
]);
}
$delta
=
$interval
->
s
;
return
Yii
::
t
(
'yii'
,
'{delta, plural, =1{a second} other{# seconds}} ago'
,
[
'delta'
=>
$delta
]);
}
}
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