I
I
Igor Vasiliev2017-06-14 08:01:55
MySQL
Igor Vasiliev, 2017-06-14 08:01:55

Change date and time dynamically since publication in Yii2, how to do it?

Hello.
--
35145c296d3e40c5b1e36f2957afb94a.png
Background: I noticed how in the social network "in contact", the date and time are updated from the moment the post was published. Roughly speaking, you scroll through your feed, the page does not reload, you write comments, put likes, and you see that the inscription changes for each block individually:

"опубликовано только что"
"опубликовано пять минут назад"
"опубликовано вчера в 13:16"

Do not judge strictly, I decided to try to do it myself, without help and hints, I wrote a function in php that displays this or that message, wrapped the page in Pjax, and installed a javascript function that updated the page, so in each block the inscriptions changed dynamically. And everything seems to be great, BUT - the fun started when the script could not cope with displaying tiles with pictures, and everything moved out, later I realized that it would be impossible to set both "likes" and "comments" !!!
My idea to implement such an idea turned into a nightmare)))
But I did not despair, and realized that php and Pjax should be abandoned, and a javascript function should be written that, by block id, will display the status of the publication period. It also came to the realization that the conditions for changing the unix date should be based on the function of calculating the time from the date of publication.
I did something similar in php to calculate the days between two dates, there is not a drop of doubt that there is such a solution in javascript, why did I decide to turn to it? Because the load on the server will be less, given that there can be more than 100 entries on one page, and each will update the current due date from the time of publication. It's scary to imagine what the load on the server will be, given the dynamics of viewing, editing, commenting, updating the date, "likes", but still I wonder if I can master such functionality.
======================================
Question:
There is an example of a dynamic date that shows how to implement update with today's date. I will try to update the statute of limitations of the date, taking it from the database in unix format. How to extract the date, calculate the amount of time (difference), display the content - I know.
But how to implement this for each block separately?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
planc, 2017-06-14
@Isolution666

https://momentjs.com/

M
Maxim Fedorov, 2017-06-14
@qonand

There can be at least 1000 blocks on the page, but the user always sees only a certain number of them. Therefore, for good, you need to update the statute of limitations not for all the blocks displayed on the page, but only for those that are visible to the user. Accordingly, you need to:
1. Determine (for example, when scrolling the page) which blocks are currently visible to the user
2. Define an array of selectors for each block
3. Loop through this array and recalculate the date for each block

M
Maxim Timofeev, 2017-06-14
@webinar

And what does yii have to do with it? This is a js question. Everything that happens in the browser is js. Yii stores and retrieves time data from the database and does not change it. Changes them js, timer or scroll.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question