代码: 全选
#!/bin/bash
begintime="2008-08-20 09:50:00"
nowtime="$(date '+%F %T')"
ddiff=$(($(date -d "$nowtime" +%s)-$(date -d "$begintime" +%s)))
diffyear=$(($(date -d "$nowtime" +%Y)-$(date -d "$begintime" +%Y)))
diffmonth=$(($(date -d "$nowtime" +%m|sed 's/^0//g')-$(date -d "$begintime" +%m|sed 's/^0//g')))
diffday=$(($(date -d "$nowtime" +%d|sed 's/^0//g')-$(date -d "$begintime" +%d|sed 's/^0//g')))
diffhour=$(($(date -d "$nowtime" +%H|sed 's/^0//g')-$(date -d "$begintime" +%H|sed 's/^0//g')))
diffminute=$(($(date -d "$nowtime" +%M|sed 's/^0//g')-$(date -d "$begintime" +%M|sed 's/^0//g')))
diffsecond=$(($(date -d "$nowtime" +%S|sed 's/^0//g')-$(date -d "$begintime" +%S|sed 's/^0//g')))
if [ $diffsecond -lt 0 ]
then
((diffminute--))
((diffsecond = diffsecond+60))
fi
if [ $diffminute -lt 0 ]
then
((diffhour--))
((diffminute = diffminute+60))
fi
if [ $diffhour -lt 0 ]
then
((diffday--))
((diffhour = diffhour+24))
fi
if [ $diffday -lt 0 ]
then
((diffmonth--))
case $(date -d "$begintime" +%m|sed 's/^0//g') in
1|3|5|7|8|10|12)
((diffday = diffday+31))
;;
4|6|9|11)
((diffday = diffday+30))
;;
2)
if [ $(($(date -d "$nowtime" +%Y)%4)) -eq 0 ]
then
((diffday = diffday+29))
else
((diffday = diffday+28))
fi
;;
esac
fi
if [ $diffmonth -lt 0 ]
then
((diffyear--))
((diffmonth = diffmonth+12))
fi
allmonth=$((diffyear*12+diffmonth))
allday=$(($(($ddiff/(3600*24)))))
allhour=$(($(($ddiff%(3600*24)))/3600))
allminute=$(($(($ddiff%3600))/60))
allsecond=$(($(($ddiff%3600))%60))
echo "宝贝现在已经 $allday天 $allhour小时 $allminute分 $allsecond秒"
echo "第 $allmonth个月 $diffday天 $diffhour小时 $diffminute分 $diffsecond秒"
echo "$diffyear年 $diffmonth个月 $diffday天 $diffhour小时 $diffminute分 $diffsecond秒"