LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Blogs > druuna
User Name
Password

Notices



- - - - - As of February 1st 2014 this blog is no longer updated - - - - -
Rate this Entry

Date and time functions

Posted 12-09-2013 at 12:34 PM by druuna
Updated 01-31-2014 at 04:25 AM by druuna (Small changes to layout)
Tags date, seconds, time

A handful of date and time related functions that might come in handy.
  • Convert a date or time string to seconds from epoch
PHP Code:
# ------------------------------------------------------------------ #
# Function : dateToSecs
# Syntax   : dateToSecs "[date]"
# Convert a date or time string to seconds from epoch.
# ------------------------------------------------------------------ #
function dateToSecs() {
  [[ $
# -eq 0 ]] && date --utc '+%s'
  
[[ $# -ne 0 ]] && date --utc --date "$1" '+%s'

Code:
$ dateToSecs "2013-12-09 20:00"
1386619200

$ dateToSecs 2013-12-09
1386547200

$ dateToSecs 20:00
1386619200

$ dateToSecs
1386610113
  • Convert seconds from epoch to a date string
PHP Code:
# ------------------------------------------------------------------ #
# Function : secsToDate
# Syntax   : secsToDate <seconds>
# Convert seconds from epoch to a date string.
# ------------------------------------------------------------------ #
function secsToDate() {
  
date --utc --date @$+'%Y-%m-%d %T'

Code:
$ secsToDate 1386619200
2013-12-09 20:00:00
  • Calculate difference in seconds between 2 dates/times
PHP Code:
# ------------------------------------------------------------------ #
# Function : secsBetweenDates
# Syntax   : secsBetweenDates "<date1>" "<date2>"
# Calculate difference in seconds between 2 dates/times
# ------------------------------------------------------------------ #
function secsBetweenDates() {
  
dateOne=$( date --utc --date "$1" '+%s' )
  
dateTwo=$( date --utc --date "$2" '+%s' )
  
dateDiff=$(( dateOne dateTwo ))
  echo 
"${dateDiff/-/}"

Code:
 $ secsBetweenDates "2013-12-09 19:00" "2013-12-09 20:00"
3600

$ secsBetweenDates 2013-12-08 2013-12-09
86400

$ secsBetweenDates 20:00 20:10
600
  • Convert seconds to MM:SS or HH:MM:SS or DD HH:MM:SS
PHP Code:
# ------------------------------------------------------------------ #
# Function : secsToTime
# Syntax   : secsToTime [d|h|m] [seconds]
# Convert seconds to MM:SS or HH:MM:SS or DD HH:MM:SS
# ------------------------------------------------------------------ #
function secsToTime() {
  
# d -> DD HH:MM:SS
  
[[ "$1" == "d" ]] && { (( d=${2}/86400 )) ; 
                         (( 
h=(${2}%86400)/3600 )) ; 
                         (( 
m=(${2}%3600)/60 )) ; 
                         (( 
s=${2}%60 )) ; 
                         
printf "%02d %02d:%02d:%02d\n" $d $h $m $s ; }
  
# h -> HH:MM:SS
  
[[ "$1" == "h" ]] && { ((h=${2}/3600)) ; 
                         ((
m=(${2}%3600)/60)) ; 
                         ((
s=${2}%60)) ; 
                         
printf "%02d:%02d:%02d\n" $h $m $s ; }
  
# m -> MM:SS
  
[[ "$1" == "m" ]] && { ((m=${2}/60)) ; 
                         ((
s=${2}%60)) ; 
                         
printf "%02d:%02d\n" $m $s ; }

Code:
$ secsToTime m 678
11:18

$ secsToTime h 4278
01:11:18

$ secsToTime d 90678
01 01:11:18
  • Legal date formats for all the above
PHP Code:
legal date formats             # date command equivalent
   
HH                          # '+%H'
   
HH:MM                       # '+%R'
   
HH:MM:SS                    # '+%T'
   
YYYY-MM-DD                  # '+%F'
   
YYYY-MM-DD HH               # '+%F %H'
   
YYYY-MM-DD HH:MM            # '+%F %R'
   
YYYY-MM-DD HH:MM:SS         # '+%F %T'
   
YYYY/MM/DD                  # '+%Y/%m/%d'
   
YYYY/MM/DD HH               # '+%Y/%m/%d %H'
   
YYYY/MM/DD HH:MM            # '+%Y/%m/%d %R'
   
YYYY/MM/DD HH:MM:SS         # '+%Y/%m/%d %T' 
Posted in General
Views 440 Comments 0
« Prev     Main     Next »

  



All times are GMT -5. The time now is 08:16 AM.

Main Menu
Advertisement

Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration