Linux Blog

timestamps in the shell

Filed under: Shell Script Sundays — at 12:02 pm on Sunday, March 2, 2008

Time and Date functions are very important when writing shell scripts. I mostly use them for logging reasons, for example to know when something was run last. As much as I dislike time stamps they are still used (at least for now) and therefore I am giving and example.

I am unsure of a way to get a time stamp in Bash. If you have PHP installed you can do the following to get a UNIX time stamp (suitable for inserting into a DB):

php -r 'echo time()."\n";'

php -r executes PHP code inside quotes. the time() function just creates a time stamp for the current time. If you need to format a string based on a time stamp you can use the date() function. Here is an example of turning a timestamp into a readable string:

bash-3.1$ php -r 'echo date("l dS \of F Y h:i:s A","1204476759")."\n";'
Sunday 02nd of March 2008 11:52:39 AM

Take a look at the date() function page on if you wish to use this method of using time stamps in the shell.

If any one has any other methods of using time stamps in the shell or needs any help as usual leave a comment :)

Man Pages for commands in this post »




Comment by Woody

March 13, 2008 @ 4:51 pm

Bah.. Just use $(date +%s)


Comment by Owen

March 14, 2008 @ 4:40 pm

I totally skipped that in the date man pages. Whoops, thats a much better way of doing it. Thanks Woody!

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>