PHP timestamp usage example code_PHP tutorial

WBOY
Release: 2016-07-21 15:51:31
Original
1276 people have browsed it

We will definitely encounter this situation: Bank A and Bank B open your account almost at the same time and see the original deposit of 1,000 yuan in your account, and then both banks want to add a deposit of 500 yuan to your account. Then, Bank A changes the amount from 1,000 yuan to 1,500 yuan, and at the same time, Bank B also changes the amount from 1,000 yuan to 1,500 yuan. That's bad! In the end, you end up with only 1,500 yuan in your bank account instead of the 2,000 yuan you should have, which is equivalent to a loss of 500 yuan! This is where modifications without locking the data cause serious problems. However, we can solve this problem neatly through timestamps.

Let’s look at the idea:

Create the timestamp field timestamp 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 performs a save operation, it compares the previously read timestamp 123456 with the timestamp in the current table. If they are consistent, the save is allowed and a new timestamp is generated. For example, 456789 replaces the original timestamp 123456 in the table.
What benefits will this bring?

Let’s look at the situation at the beginning: Bank A and Bank B opened your account almost at the same time and saw the original deposit of 1,000 yuan in your account. At the same time, the two banks read the time at the same time. Stamp 123456, and there will be a difference. When Bank A changes the 1,000 yuan to 1,500 yuan and saves the disk, the system will compare whether the previous timestamp 123456 is consistent with the timestamp in the table when saving. Obviously, it should be consistent now. , then the save is allowed and a new timestamp 456789 is generated to replace the old timestamp 123456. Next, Bank B also changed 1,000 yuan to 1,500 yuan and saved it. The system compared the previous timestamp 123456 to see if it was consistent with the timestamp in the table when saving, and found that the previous timestamp 123456 was different from the current timestamp 456789. , the system refuses to save and requires refreshing the data. Then after the data is refreshed, the 1,000 yuan has become 1,500 yuan because Bank A previously deposited 500 yuan. Then Bank B will change the 1,500 yuan to 2,000 yuan and save again. The system allows it. In this way, we avoid errors caused by repeatedly modifying data!

It’s a bit like a tongue twister, I hope everyone understands what I mean~

Finally, let’s take a look at some operation codes for timestamps in PHP.

Get timestamp
$timestamp=time();
echo $timestamp;
?>
SQL statement to update timestamp:
update table name set field name=$timestamp where condition=value;
Author: Sunec
Original: Cenus Blog
All rights reserved. When reprinting, the author, original source and this statement must be indicated in the form of a link.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319129.htmlTechArticleWe will definitely encounter a situation where Bank A and Bank B open your account and see you almost at the same time There is an original deposit of 1,000 yuan in your account, and then both banks want to add 5...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!