Tutorials PHP Dates and Timestamps

Dates and Timestamps

This week's lesson will be about dates.

How do I store dates?

The easiest way is to store dates in timestamp format. Timestamp is a number that has counted every second since 1970. Since it is just a number, it is a lot easier to manipulate the date format.

How do I get the current timestamp and date?

// Timestamp format$timestamp = time();// Date format$date("M j Y, g:i a");

How do I convert timestamp into a valid date format?

You need to tell it what format you want the date to be. No point in trying to copy and paste the whole chart so just go here to find date formats: php.net

// Outputs "Mar 8 2011, 4:49 pm"$timestamp = 1299620958;$date("M j Y, g:i a",$timestamp);

Can I change a date to timestamp?

This works with anything that is in a correct date format.

// Outputs "1284091200"$timestamp = strtotime("10 September 2010");

Can't I just store all my dates in date format?

It's possible to keep your dates in date format in your database, but timestamps are more efficient. Timestamp is just a number and it's a bit easier on the server to store an integer than it is to store a variable of both numbers and letters.

Posted by on . Category: PHP


No comments posted yet

You need to register or login to post new comments.