John Davidson

How consistent is PHP hrtime()?

0 comments
Message:


I'm currently using hrtime() in one of my project, a chat system (still in development stage).


On each message sent, I store the hrtime(true) value along with the message into database.


So recipient will request for new messages based on last message hrtime stored in app localStorage.
like for example: select*from messages_table where message_hrtime>$last_hrtime and message_to=$me


It has been working alright until today, thrn I noticed new messages
hrtime(true) value inserted into DB decreased which make new messages not to be received.



Notice the column message_hrtime decreased on id 35


And according to this article
https://thephp.cc/articles/high-resolution-monotonic-timer, that hrtime() is ever increasing.


Now confused if the hrtime(true) is consistent.


PHP 7.3



My Answer:
() > last_message_hrtime

The hrtime() function in PHP is consistent in terms of providing high-resolution time measurements. It returns the current time in nanoseconds since an unspecified starting point. However, it is important to note that the starting point may vary depending on the system and may not be consistent across different platforms.

In your chat system, using hrtime() to store the time of messages and compare them for retrieving new messages should work reliably within the same system. Just make sure to handle any potential differences in starting points if your application needs to be deployed on different platforms.

Overall, hrtime() can be a useful tool for precise time measurements in PHP, but it's always a good idea to test and validate its behavior in your specific use case to ensure consistency.

Rate this post

5 of 5 based on 1079 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us