---
title: "函数：将秒数转化为相对时间"
author: "c_c"
date: "2009-09-17T09:33:18Z"
tags: []
categories: ["数据库"]
slug: "untitled"
draft: false
lastmod: "2009-09-17T09:33:18Z"
id: 470202
url: "https://waddy.iteye.com/blog/470202"
---

函数名：get\_time\_zdp

参数：seconds 秒数

功能：将秒数转化为相对时间

举例：select get\_time\_zdp(2\*60\*60\*24\*30+2\*60+3\*60\*60) from dual;         

         --2个月3小时2分0秒

 

代码：

```
create or replace function get_time_zdp(
  seconds in number
) 
return varchar2 as
  temp   number;
  years  number;
  months number;
  days number;
  hour   number;
  minit  number;
  sec    number;
  RET    VARCHAR2(100);
begin
  temp :=seconds;    
  if ( round(temp/(60*60*24*30*12))>0) then  
      years  :=floor(temp/(60*60*24*30*12));    --一年按12个月记 
             temp :=temp -years*(60*60*24*30*12);
             dbms_output.put_line(temp);
      RET :=RET||years||'年';
  end if;
  if( floor(temp/(60*60*24*30))>0) then
      months :=floor(temp/(60*60*24*30));       --一个月按30天记
             temp :=temp -months*(60*60*24*30);
             dbms_output.put_line(temp);
      RET :=RET||months||'个月';
  end if;
  if( floor(temp/(60*60*24))>0) then
      days :=floor(temp/(60*60*24));
           temp :=temp -days*(60*60*24);
           dbms_output.put_line(temp);
      RET :=RET||days||'天'; 
  end if; 
  if( floor(temp/(60*60))>0) then
      hour :=floor(temp/(60*60));
           temp :=temp -hour*(60*60);
           dbms_output.put_line(temp);
      RET :=RET||hour ||'小时';
  end if;
  if( floor(temp/60)>0) then
      minit :=floor(temp/60);
           temp :=temp -minit*60;
           dbms_output.put_line(temp);
      RET :=RET||minit ||'分';
  end if;
  
  RET :=RET||temp || '秒';
  return RET;
end;
```