Hello.

I decided to make a system on the site that will count how much time has passed since the material was published (Example: 1 hour ago, 2 hours ago, 1 day ago, etc.). And so the question was how can this be realized? As long as there are options either to calculate right on the page, or to make it all a “crown” which will put the whole thing in the database and then just output. There is a lot of materials. How will it be more efficient and faster for server operation?

Thank you in advance.

ps. Do not mind listening to other options as it can be done.

    2 answers 2

    in the table, make a timestamp field of type INTEGER.

    Write the date there f-mktime ()

    well, then just compare, you can even type one by the second

    Changed 1 minute and 23 seconds ago.

    or

    Modified 1 year 3 months 4 days 12 hours 5 minutes 11 seconds ago

    as you want.

    This in real time will be considered. And in order not to load a server, you can launch a cron by page and cache the result with the result, or cache the result separately, although of course it is better if the content does not change that page completely.

      In MySQL, there should be a field with the usual date-time. Calculation of the time that has passed since the publication, should make PHP at the time of the page output to the browser. It is at this point that the information is relevant. That is, to use crowns for this task is meaningless.

      If you need a dynamic counter, then you need to do this at the JavaScript level. PHP displays the publication time on the page, reads it in the JavaScript browser immediately and, for example, once a second does the calculation and output of a new value.

      Total: server load tends to zero (one-time calculation at the time of the withdrawal).