echo "devil";

How to create a DateTime object

15.07.2010 09:30 | PHP

Since PHP 5.2 was the amazing DateTime class available and in 5.3 updated with new amazing functions. Well, what is the so amazing about it? It's the object-oriented way of handling dates in PHP! If you need another reason: It's easy to use and offer a lot of options!

Here are a few examples of the class in use:

$objDateTime = new DateTime('2010-06-01 13:15:30');

echo $objDateTime->getTimestamp(); // Echo the unix timestamp
echo $objDateTime->format('Y-m-d'); // Echo a formatted date

$objDateTimeZone =  $objDateTime->getTimezone(); // Get the timezone relative to the date/time as object

The clever PHP programmer already noticed that the constructor of DateTime accept the same format that you usually use when calling the strtotime() function.

Manipulating, getting and setting dates was never easier. I advise you to play around with this class and see if it might be able to lighten your work a little. It sure did for me.

Tags: timestamp, datetime, date, php function

« Back «

Share |

RSS feed