Home  >  Article  >  Backend Development  >  An example of a PHP custom function to calculate the remaining time

An example of a PHP custom function to calculate the remaining time

WBOY
WBOYOriginal
2016-07-25 08:55:40912browse
  1. /**

  2. * Calculate remaining time
  3. * by bbs.it-home.org
  4. */

  5. public function gettime($time_s,$time_n){

  6. $time_s = strtotime($time_s);
  7. $time_n = strtotime($time_n);
  8. $strtime = '';
  9. $time = $time_n-$time_s;
  10. if($time >= 86400){
  11. return $strtime = date('Y-m-d H:i:s',$time_s);
  12. }
  13. if($time >= 3600){
  14. $strtime .= intval($time/3600).'小时';
  15. $time = $time % 3600;
  16. }else{
  17. $strtime .= '';
  18. }
  19. if($time >= 60){
  20. $strtime .= intval($time/60).'分钟';
  21. $time = $time % 60;
  22. }else{
  23. $strtime .= '';
  24. }
  25. if($time > 0){
  26. $strtime .= intval($time).'秒前';
  27. }else{
  28. $strtime = "时间错误";
  29. }
  30. return $strtime;
  31. }

复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn