The DateTime class sure has some handy methods and seems overall superior to the native PHP date functions like strtotime
, mktime
and strftime
(and more). But is there any drawback or a reason why I shouldn't use it ?
The only reason I can think of is that it might be more expensive to create a whole instance of a class than just using a function.
- Would you agree with that ?
- Does it make sense at all to use a DateTime object for simple stuff?
- Are there any other drawbacks ?
It seems a bit confusing to switch between those two options all the time, so I'd like to have clearance what I should prefer doing.
Two examples for my decision would be:
- Converting a date to a localized value
- Calculating the time between two dates
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…