#Notes on portability
What I've written above is a straight Bash answer; my personal preference for PS1 is for it to be different for each shell, so I'm reminded which one I'm in. Thus, absolute consistency is an explicit non-goal for me.
However, the principle of creating local variables for the elements of the prompt works well for portability: we can isolate the shell-specific parts like this:
local DATE='\D{%Y-%m-%d %H:%M:%S}'
test ${BASH_VERSION+1} || DATE='$(date +%Y-%m-%d\ %H:%M:%S)'
or
local WHOAMI=$(whoami)
local WHERE='$(pwd)'
local HOSTNAME=$(hostname)
local DATE='$(date +%Y-%m-%d\ %H:%M:%S)'
local LAST_RET='${?#0}'
if [ ${BASH_VERSION:-} ]
then
# Bash-specific overrides
WHOAMI='\u'
WHERE='\w'
HOSTNAME='\h'
DATE='\D{%Y-%m-%d %H:%M:%S}'
fi
#Afterthought
I should have used lowercase for the variable names above: that is the easiest way to ensure no conflict with the shell's own variables or with well-known environment variables.