Welcome Guest, Not a member yet? Register   Sign In
does anyone use the php 5.2+ DateTime class?

I was made aware of the DateTime class[/code] when i read the php 5.3 documentation for the new date_add, date_diff and date_sub functions.

The date_create function accepts the date strings that strtotime can accept and the strtotime parameter to modify is been put in the function date_modify. So from php 5.2 on you can do
$datetime = date_create();
date_modify("+1 month");
// or
$datetime = new DateTime();
$datetime->modify("+1 month");
As you can see from the snippet the methods of the DateTime class are also accessible as functions.

in php 5.3 the getTimestamp method is added, this is useful for developers who store the dates as a date(time) type in the database then you don't have to rely on the database functions to make a unix timestamp.

As i mentioned before the strtotime function is capable of all these things so the only thing those functions are good for is readability.

The date_diff function is something that was missed in php by many developers but in php 5.3 you can do
$datetime = new DateTime('2008-08-03 14:52:10');
echo $datetime->diff(new DateTime('2008-08-04 14:52:10'))->format('%R%a');
Which returns +1. The diff method returns a [url=http://be.php.net/manual/en/class.dateinterval.php]DateInterval
object. So if you want to output the result with a function you have to use date_interval_format.

And now for the title, does anyone uses the DateTime class already and how many will start using it after reading this?

Theme © iAndrew 2016 - Forum software by © MyBB