PHP Timestamp uses instance code _php basics

Source: Internet
Author: User
We are bound to encounter a situation where Bank A and bank B open your account almost at the same time and see the original 1000 Yuan deposit on your account, and both banks want to add a 500 yuan deposit to your account. Then, bank A will change 1000 yuan to 1500 yuan, at the same time, Bank B will also change 1000 yuan to 1500 yuan. This is going to be a bad thing! Finally, your bank account at the end of only 1500 yuan instead of the supposedly 2000 yuan, equal to the loss of 500 yuan in vain! This is the serious problem caused by the modification without locking the data. However, we can solve this problem skillfully by time stamp.

Let's look at the idea:

Create a timestamp field timestamp in the Bank Account table, set to 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, it makes a comparison between the previously read timestamp 123456 and the timestamp in the table if it is consistent, then allows the disk to be saved, and then generates a new timestamp such as a 456789 substitution table with a timestamp of 123456.
What good will it do?

Let's take another look at the beginning of the situation: Bank A and Bank B open your account almost at the same time and see your account of the original 1000 yuan deposit, while two banks read the time stamp 123456, then there is a difference, when bank A to change 1000 yuan to 1500 yuan, save, The system compares the previous timestamp 123456 with the timestamp in the table in the disk, and obviously it should now be consistent, then allow the disk to be saved and generate a new timestamp 456789 to replace the old timestamp 123456. Next, B Bank will also be 1000 yuan modified to 1500 yuan, save the system compared to the previous time stamp 123456 is in the table with the time stamp consistent, found that the previous timestamp 123456 has been compared with the current timestamp 456789, the system refused to save, request to refresh data, Then the data refreshed after 1000 yuan has been deposited by a bank of 500 yuan and became 1500 yuan, then B Bank will be 1500 yuan on the basis of 2000 yuan, save again, the system allows. In this way, we avoid the errors caused by repeated modification of the data!

Some like tongue twisters, I hope you have understood my meaning ~

Finally, let's look at some of the action code for timestamps in PHP.

Get time stamp
<?php
$timestamp =time ();
Echo $timestamp;
?>
To update a timestamp SQL statement:
Update table name set field name = $timestamp WHERE condition = value;
Author: Sunec
Original download: Cenus Blog
All rights reserved. Reprint must be linked to the form of the author and the original source and this statement.

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.