$partsOfTime[] = generatePartialString(array('time' => $difference->d, 'unit' => 'day')); } if ($difference->h > 0) { $partsOfTime[] = generatePartialString(array('time' => $difference->h, 'unit' => 'hour')); } if ($difference->i > 0) { $partsOfTime[] = generatePartialString(array('time' => $difference->i, 'unit' => 'minute')); } if ($difference->s > 0) { $partsOfTime[] = generatePartialString(array('time' => $difference->s, 'unit' => 'second')); } $humanReadableString = implode(" ", $partsOfTime); return $humanReadableString; } $timeInPast = strtotime('1978-04-08 21:30:15'); $timeInFuture = strtotime('2032-04-08 21:30:15'); // @formatter:off echo ' <br/>179: ', sprintf('The incident has taken place %1$s.', ago(strtotime($timeInPast))); echo ' <br/>182: ', sprintf('The incident is going to take place %1$s.', ago(strtotime($timeInFuture))); echo ' <br/>187: ', sprintf('The incident has taken place %1$s.', _ago(strtotime($timeInPast), 0)); echo '<br/>191: nullpunkt: ', date('YmdHis', 0); echo '<br/>192: PHP lõpp: ', date('YmdHis', PHP_INT_MAX); echo '<br/>192: ', sprintf('The incident is going to take place %1$s.', _ago(strtotime($timeInFuture), 1)); echo '<br/> 197: ', sprintf('The Difference between 2016-11-11 16:25:00 and now is %1$s', findDifference(array('timestampInPast' => '2015-10-11 16:25:00'))); echo '<br/> 203 :', sprintf('The difference between 2016-11-11 16:25:00 and now is %1$s', findDifference(array('timestampInFuture' => '2018-11-11 16:25:00'))); $datetime1 = new DateTime('2009-10-14'); $datetime2 = new DateTime('2009-10-13'); $interval = $datetime2->diff($datetime1); echo ' <br/>213: ', $interval->invert; echo ' <br/>214: ', \pstk\string::translate('minute'); // @formatter:on
if ($difference->h > 0) { $partsOfTime[] = generatePartialString(array('time' => $difference->h, 'unitInSingular' => \pstk\String::translate('hour'), 'unitInPlural' => \pstk\String::translate('hours'))); } if ($difference->i > 0) { $partsOfTime[] = generatePartialString(array('time' => $difference->i, 'unitInSingular' => \pstk\String::translate('minute'), 'unitInPlural' => \pstk\String::translate('minutes'))); } if ($difference->s > 0) { echo ' 193 '; $partsOfTime[] = generatePartialString(array('time' => $difference->s, 'unitInSingular' => \pstk\String::translate('second'), 'unitInPlural' => \pstk\String::translate('seconds'))); } // @formatter:on $humanReadableString = implode(" ", $partsOfTime); // echo ' <br/>201: ', $humanReadableString; return $humanReadableString; } $_SESSION['language'] = 'et_EE'; $timeInPast = strtotime('2016-2-3 5:8:9'); $timeInFuture = strtotime('2032-04-08 21:30:15'); // @formatter:off echo ' <br/>38: ', sprintf('The incident has taken place %1$s.', ago(strtotime($timeInPast))); echo ' <br/>85: ', sprintf('The incident is going to take place %1$s.', ago(strtotime($timeInFuture))); echo ' <br/>89: ', sprintf('The incident has taken place %1$s.', _ago(strtotime($timeInPast), 0)); echo ' <br/>100: nullpnkt: ', date('YmdHis', 0); echo ' <br/>100: PHP lõpp: ', date('YmdHis', PHP_INT_MAX); echo ' <br/>222: PHP algus: ', date('YmdHis', -PHP_INT_MAX); echo ' <br/>223: PHP algus: ', date('YmdHis', PHP_INT_MAX + PHP_INT_MAX); echo ' <br/>93: ', sprintf('The incident is going to take place %1$s.', _ago(strtotime($timeInFuture), 20)); echo ' <br/>155: ', sprintf('The difference between 2015-11-11 16:25:00 and now is %1$s.', findDifference(array('timestampInPast' => '2015-10-11 16:25:00'))); echo ' <br/>197: ', sprintf('The difference is %1$s.', findDifference(array())); echo ' <br/>237: ', -4 == true; // @formatter:on
* number of units * @return string How much time ago? */ function _ago($tm, $rcs = 0) { $cur_tm = time(); $dif = $cur_tm - $tm; $pds = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade'); $lngh = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600); for ($v = sizeof($lngh) - 1; $v >= 0 && ($no = $dif / $lngh[$v]) <= 1; $v--) { } if ($v < 0) { $v = 0; } $_tm = $cur_tm - $dif % $lngh[$v]; $no = floor($no); if ($no != 1) { $pds[$v] .= 's'; } $x = sprintf("%d %s ", $no, $pds[$v]); if ($rcs > 0 && $v >= 1 && $cur_tm - $_tm > 0) { $x .= _ago($_tm, --$rcs); } return $x; } $_SESSION['language'] = 'et_EE'; $timeInPast = '2016-2-3 5:8:9'; $timeInFuture = strtotime('2032-04-08 21:30:15'); // @formatter:off echo ' <br/>93: ', sprintf('The difference is: %1$s.', findDifference(array('timestampInFuture' => $timeInPast)));