Using PHP StrToTime to work with dates

php code on black backgroundThere are many ways in PHP to work with dates. My favorite is StrToTime, because I can store the long number (Unix Timestamp) representing the number of seconds since Jan 01, 1970 in the database. Then its easy to format it in any which way I like. Maybe one place I want to show the date but in another place I want the time shown too, and I also want to be able to give the user the ability to format the timestamp for their locale. All of this is possible with StrToTime. Yet it can do so much more. For example I can do things like tell it I want -30 days in the past or +3 years in the future. This makes the logic a lot easier.

echo date("jS F, Y", strtotime("11.12.10"));// outputs 10th December, 2011echo date("jS F, Y", strtotime("11/12/10"));// outputs 12th November, 2010echo date("jS F, Y", strtotime("11-12-10"));// outputs 11th December, 2010 if(strtotime($date_from_db) >= strtotime("NOW")) echo "error";//will return an error if the data in the db is greater than or equal to NOW (todays date and the time it is right now)

Tags: PHP