25 June 2010

This tutorial is about using and converting timestamps in PHP, witch are common formats of storing some timeperiod in database.

Timestamp is used so commonly in databases(like mysql), because you can make it default value and even renew the time on every update. Format might be YYYYMMDDHHMMSS, but there are others like YYYY-MM-DD HH:MM:SS that are probably converted from Unix timestamps. Timestamp is more of a database thing than PHP thing, but you get the data and have to do something with it. This tutorial shows some things you can do with timestamps in php.

Converting timestamps to more user friendly format:

Conversion is simple, you have to use PHP's date function. The first parameter is output time fromat and the second is integer Unix timestamp. So you might want to convert your timestamp to it by using strtotime function.
Source code viewer
  1. $timestamp = '2010-06-21 09:45:12';
  2. echo date('j F, Y', strtotime($timestamp);
  3. //The output is: 21 June, 2010
Programming Language: PHP
As you can see mutch more user friendly format, it doesn't include time part, only date part. You can find more about making the fromat string(first variable of fuction) from php.net.

Making timestamp for database:

To insert time string to database, use the same principle of converting time. You can also use other PHP commands like time and mktime that return Unix timestamp.