note 121908 added to function.time
| From: | APM@php.net | Date: | Thu, 23 Nov 2017 15:26:46 +0000 |
| Subject: | note 121908 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209209@lists.php.net to get a copy of this message | ||
A better way to get a nice time-format (1 year ago, 2 months until) without all the trailing months,
days, hours, minutes, seconds in the result is by using the DateTime format and using the date_diff
function as they both does most of the heavy lifting for you
Function below as example
<?php
// Get time interval function
function getTimeInterval($date) {
// Set date as DateTime object
$date = new DateTime ($date);
// Set now as DateTime object
$now = date ('Y-m-d H:i:s', time()); // If you want to compare two dates you both
provide just delete this line and add a $now to the function parameter (ie. function
getTimeInterval($date, $now))
$now = new DateTime ($now);
// Check if date is in the past or future and calculate timedifference and set tense
accordingly
if ($now >= $date) {
$timeDifference = date_diff ($date , $now);
$tense = " ago";
} else {
$timeDifference = date_diff ($now, $date);
$tense = " until";
}
// Set posible periods (lowest first as to end result with the highest value that isn't
0)
$period = array (" second", " minute", " hour", "
day", " month", " year");
// Set values of the periods using the DateTime formats (matching the periods above)
$periodValue= array ($timeDifference->format('%s'),
$timeDifference->format('%i'), $timeDifference->format('%h'),
$timeDifference->format('%d'), $timeDifference->format('%m'),
$timeDifference->format('%y'));
// Loop through the periods (ie. seconds to years)
for ($i = 0; $i < count($periodValue); $i++) {
// If current value is different from 1 add 's' to the end of current period
(ie. seconds)
if ($periodValue[$i] != 1) {
$period[$i] .= "s";
}
// If current value is larger than 0 set new interval overwriting the lower value that
came before ensuring the result shows only the highest value that isn't 0
if ($periodValue[$i] > 0) {
$interval = $periodValue[$i].$period[$i].$tense; // ie.: 3 months ago
}
}
// If any values were larger than 0 (ie. timedifference is not 0 years, 0 months, 0 days, 0
hours, 0 minutes, 0 seconds ago) return interval
if (isset($interval)) {
return $interval;
// Else if no values were larger than 0 (ie. timedifference is 0 years, 0 months, 0 days, 0
hours, 0 minites, 0 seconds ago) return 0 seconds ago
} else {
return "0 seconds" . $tense;
}
}
// Ex. (time now = November 23 2017)
getTimeInterval("2016-05-04 12:00:00"); // Returns: 1 year ago
getTimeInterval("2017-12-24 12:00:00"); // Returns: 1 month until
?>
----
Server IP: 208.43.231.9
Probable Submitter: 81.171.52.228
----
Manual Page -- http://php.net/manual/en/function.time.php
Edit -- https://master.php.net/note/edit/121908
Del: integrated -- https://master.php.net/note/delete/121908/integrated
Del: useless -- https://master.php.net/note/delete/121908/useless
Del: bad code -- https://master.php.net/note/delete/121908/bad+code
Del: spam -- https://master.php.net/note/delete/121908/spam
Del: non-english -- https://master.php.net/note/delete/121908/non-english
Del: in docs -- https://master.php.net/note/delete/121908/in+docs
Del: other reasons-- https://master.php.net/note/delete/121908
Reject -- https://master.php.net/note/reject/121908
Search -- https://master.php.net/manage/user-notes.php