PHP timestamp using instance code

Source: Internet
Author: User

We will surely encounter this situation: Bank A and Bank B open your account almost simultaneously and see the original 1000 yuan deposit in your account, then both banks want to add a deposit of 500 yuan to your account. Then, Bank A will change 1000 yuan to 1500 Yuan, and Bank B will also change 1000 yuan to 1500 yuan. This is terrible! In the end, your bank account has only 1500 Yuan instead of 2000 yuan, which is equivalent to a loss of 500 yuan! This is a serious problem caused by modification without locking the data. However, we can use timestamps to solve this problem.

Let's look at the ideas:

Create a timestamp field in the bank account table and set it to the text type varchar.
When Bank A reads the deposit field in the account table, it also reads the timestamp field, such as 123456.
When Bank A modifies the deposit value and saves the disk, it compares the previously read timestamp 123456 with the timestamp in the current table. If the timestamp is the same, it is allowed to store the disk, then generate a new timestamp, such as 456789, to replace the original timestamp 123456 in the table.
What are the benefits of doing so.

Let's take A look at the first situation: Bank A and Bank B opened your account almost simultaneously and saw the original 1000 yuan deposit in your account, at the same time, the two banks read A timestamp of 123456 at the same time, and then there is A difference. When Bank A changes 1000 yuan to 1500 Yuan, the system will compare whether the previous timestamp 123456 is consistent with the timestamp in the table when saving the disk. Obviously, it should be consistent now, so the disk can be stored, the new timestamp 456789 is generated to replace the old timestamp 123456. Next, Bank B also changed the 1000 yuan to 1500 yuan for disk storage. The system compares the previous timestamp 123456 with the timestamp in the table during disk storage, it is found that the previous timestamp 123456 is different from the current timestamp 456789. The system rejects disk storage and requires data refresh, then, after the data is refreshed, the RMB 1000 has already been converted into RMB 500 because Bank A has previously saved RMB 1500, so Bank B will replace it with RMB 1500 on the basis of RMB 2000 and store it again, the system allows. In this way, we can avoid errors caused by repeated data modification!

I hope you can understand what I mean ~

Finally, let's take a look at some of the time stamp operation code in PHP.

Obtain Timestamp
<? Php
$ Timestamp = time ();
Echo $ timestamp;
?>
SQL statement for updating timestamp:
Update table name set field name = $ timestamp where condition = value;
By Sunec
Original load: Cenus Blog
Copyright. The author and original source and this statement must be indicated in the form of links during reprinting.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.