Register Login Contact Us

I liked seeking male who like Php date comparision

Returns the difference between two DateTimeInterface objects.


meeting girls in Middletown, CT

Online: Now

About

.

Gabrielle
How old am I: 32

Views: 4362

submit to reddit

When I search and read code about time and date problems developers have, I noticed that a lot of people still use the old PHP functions like datetime or strtotime. What about using the class DateTime instead? DateTime can do all the usual Php date comparision and time operations you could ask for, and even more. Using DateTime can save a lot of time when you have to do simple, or more complex operations on dates.

The instantiation is easy: you can pass to the constructor a date correctly formatted, or nothing if you want to use the actual date and time. Here the list of supported date and time formats you can inject in the constructor. You can as well precise the timezone you want as a second argument:.

What about adding or retrieving a day, a minute, an hour of a date? You only need to use the proper formatting :.

First method

For the same result, you could as well instantiate the class DateInterval but you would have to use an interval specification which is way harder to read. In this example we created two DateTime objects.

They will receive two different dates in their constructors. In order to compare those two dates we use the method diff of the first DateTime object with the second DateTime object as argument. The diff method will return a new object of type DateInterval.

Keep in mind that you need PHP 5. You will never have to mess around with multiple functions and you will simplify your code a big time. Your colleague will praise you and your skills till the end of time. If you know other tricks and techniques with DateTimefeel free to share your knowledge in the comment section.

Less bug is always better. DateTime is a class. You can use composition to modify his behavior easily.

You need always the same date as input for your unit tests? Use your own instance of DateTime which always return the birthday of your dog! Using one class is easier to understand for your fellow developer colleagues.

It is definitely better than looking for a bunch of independent functions. DateTime Instantiation and Formatting The instantiation is easy: you can pass to the constructor a date correctly formatted, or nothing if you want to use the actual date and time.