You need two functions and a timer. First function is executed just after you hit enter on the command line, but before actual command starts. Second function is executed after command finishes, but before prompt is displayed. Timer just counts seconds since you start the shell. In
zsh these three hooks are called
bash timer's name is the same, function
precmd become a variable
PROMPT_COMMAND, but unfortunately function
preexec is missing, so you need to write it yourself (nothing extremely challenging, but not trivial either) or install already written hook from external source, e.g. https://github.com/rcaloras/bash-preexec.
Now we just need to glue all pieces together, minimal code looks like this:
Put everything in