I managed to make it count difference of two dates, but because it actually counts time difference once you get past target date it starts counting up (and I need remaining days then show overdue days with negative indicator “-” plus a note ). I have sorted the note but it only works if remaining months and days equals 0.
Note that if the only thing you care about is “is this date in the past or the future”, you dont actually need to do a diff, just say if($thetargetdate < $todaysdate).
I have seen many samples on web with diff function but none of them worked for me and the code I have posted is the only one that works? I know diff is very simple and quick way to do this job but I have struggled…
So, assuming that this code you have posted above is all you’re doing with the dates, this code can be 8 lines long.
1: Create a DateTime from your date values.
2: Create a DateTime for Now. (Hint: The parameter of the constructor is optional.)
3: Calculate the Diff of those two objects.
4: if (line1 < line2)
5: post message
6: } else {
7: post message
8: }
There is no need for a cluster muck of code gymnastics to get a date difference. The DB is more than capable of doing it with SQL. At its most basic this is all you need.
That may be true for mysql (it’s actually not true for all databases). It still needs ‘gymnastics’ because it still needs to add another (integer) field as months to the date stored, and it still needs to do NOW(), so… it still does all the same ‘gymnastics’, but does it at the database end.
Personally I find it difficult to gauge the difference between the two on so small a scale as these operations; the variance in my testing was so significant that the answer I came up with was “it depends on what my CPU feels like at the time.” Course i was running all my tests locally; across a network, on a database server that may be busier than hosting a single table on a single database… shrug
In general, my SQL-calculated tests slowed down more than PHP with the more rows that I pulled. So perhaps it’s a different variable that needs to be examined.
In any case, the code complexity doesnt change much between the two, and flexibility for additional uses and use cases favors PHP.
Well, PHP is purely in memory whereas MySQL has at least network overhead. This might be low if you’re on a local machine as @m_hutley says, but once that database server is physically separated from the server running PHP you will certainly feel that latency.
EDIT: Of course if you’re already querying the database anyway it might be easier to include that in the query as well, I was more thinking about a case where you’re just querying the database just calculate the difference between two supplied dates, that wouldn’t make sense.
Do not forget the calculation includes two dates - “lastmaint date” & “next maint date” so you get result of how many days left to text maintenance. Another point to make - “servicing interval” is set by number i.e. 3 (which indicates 3 months) and I need to calculate “next maint date” by adding 3 months to “last maint”.