How to Convert a Time Stamp to a Date in PHP
The PHP programming language has several useful functions for getting and manipulating the date and time. The PHP "time()" function is used to get the current Unix timestamp, a number representing the number of seconds since January 1, 1970 Greenwich Mean Time (GMT). This number is handy for generating unique and random numbers, but as a readable date and time, it leaves much to be desired. Luckily, the PHP "date()" function was designed to format this timestamp using a custom format.
Instructions
-
-
1
Create a variable to hold the current timestamp and assign it the value returned by the time() function:
$now = time() ;
time() takes no parameters, and returns a Unix timestamp. If you need another date relative to now, calculate the number of seconds you need to offset the timestamp and add or subtract it from $now.
For example, yesterday was 24 hours times 60 minutes times 60 seconds ago:
$yesterday = $now -- (24 * 60 * 60) ;
Also,
$tomorrow = $now + (24 * 60 * 60) ;
$nextweek = $now + (7 * 24 * 60 * 60) ; -
2
Set the default timezone. This will allow the date() function to properly convert the timestamp to the date and time for the default location. For example,
date_default_timezone_set('America/Denver') ;
See the "List of Supported Timezones" in the Resources section.
-
-
3
Construct a date formatting string matching the format you want to display or use. There are several standard formatting characters, such as 'd' for a two-digit day, 'D' for an abbreviated day name, 'F' for the full name of the month, and 'Y' for the four digit year.
For example, the string "l, F j, Y" will yield a date that looks like "Tuesday, March 30, 2010". See the "PHP.net: date" documentation linked in the References section for a full list of formatting string characters.
-
4
Call the date() function, passing the date formatting string and the timestamp as parameters:
$todaystr = date ("l, F j, Y", $now) ;
Now you can use the variable containing your formatted date ($todaystr in this case), echoing it to the page, concatenating to onto another string, or passing it to a function requiring a formatted date. There are predefined formats you can pass in for the formatting string parameter to fetch standard date-time strings. For example:
$todaystr = date (DATE_RSS, $now) ;
sets $todaystr to an RSS date format like the following:
Tue, 30 Mar 2010 15:33:01 -0600
See the "List of Supported DateTime Constants" in the Resources section for a complete list.
-
1
Tips & Warnings
If you just need a formatted date, not a Unix timestamp, set the timezone and call the date() function with just the formatting string. For example, "date("F j, Y, g:i a")" yields the current date and time formatted like "March 30, 2010, 3:33 pm". The date() function defaults to the current date and time.
The mktime() function offers an an alternative method for constructing Unix timestamps.
Some databases, such as MySQL, can convert timestamp fields into Unix timestamp format, making it easy to convert from the database's format to your own format.
References
Resources
- Photo Credit sun dial image by RT from Fotolia.com