Timestamp PHP timestamp usage example code

WBOY
Release: 2016-07-29 08:38:08
Original
1053 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 500 yuan deposit 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 a serious problem caused by modifications without locking the data. 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 such as 456789 is generated. Replace 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 timestamp 123456 at the same time. Next There is a difference. When Bank A changes 1,000 yuan to 1,500 yuan and saves the disk, the system will compare the previous timestamp 123456 to see if it is consistent with the timestamp in the table when saving. Obviously, it should be consistent now, so the save is allowed. And generate a new timestamp 456789 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. System allows. 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.

The above introduces the timestamp PHP timestamp usage example code, including timestamp content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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!