0

While reading the PHP manual for the strtotime() function I got to a "Note" with a very poor explanation - saying this:

Note:
Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.

What is meant by the "mathematical operations"?
...of course I know what mathematical operations are in general, but in what context is this meant?

And why it is not "advisable"?

Could anyone show me an example of use where it "is not advisable" and explain why?

Thanks.

John Conde
  • 217,595
  • 99
  • 455
  • 496
jave.web
  • 13,880
  • 12
  • 91
  • 125

1 Answers1

4

You can add and subtract dates to get date intervals. DateTime is better for this because it takes things like leap years and daylight savings into account for you, whereas you have to manually with strtotime().

bishop
  • 37,830
  • 11
  • 104
  • 139
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • `strtotime()` doesn't take leap years into account? – Amal Murali Feb 13 '14 at 19:37
  • It does but a developer might not. So if they start mucking with timestamps they may forgot to take a leap year into account and be off by a day. – John Conde Feb 13 '14 at 19:38
  • ot just forget daylight saving, leap seconds or whatever other corrections are done. Some of them are reflected by the linux timestamp (leap years), some are not (daylight savings), some are in-between (leap seconds - timestamps are right after the leap second, but not during). Really, you don't want to program that on your own. – Johannes H. Feb 13 '14 at 19:41
  • @JohnConde so it isnt slower or something? The advise is just about the simpleness of usage? – jave.web Feb 13 '14 at 19:48
  • The advice is to help prevent developers from making errors. Working with dates isn't as easy as it sounds. Using the proper tools for the job makes it less error prone. – John Conde Feb 13 '14 at 19:49