ComputersSoftware

Timestamps in PHP: timestamp and time calculation

Time always matters. A site that does not control the time and does not use it continuously, not that it does not have the right to life, but definitely does not apply to full-featured modern Internet resources.

PHP functions for working with time

In PHP, the time and date are represented by a set of functions that are sufficient to work without limitations, and the ability to bring time and date to a single base. The developer can always get a timestamp to record (track) events and perform date calculations.

There is no need to write your own code that controls (calculating) the number of days in a month, a leap year, a day of the week, the name of a month, etc. You can work on a project and rely entirely on PHP's ability to work with this parameter.

In principle, two functions (getTimeStamp, GetDate) are quite enough for solving any problem when dealing with a date and / or time. But the sophisticated developer can use other functions.

Essentially: in PHP, a timestamp is a kind of numeric label that takes on a new numerical value every second. Historically, this number is the number of seconds that have elapsed since midnight on January 1, 1970 .

Objective reality and the value of Timestamp

In programming, there have always been many epoch-making dates. As any decent programmer remembers the date of its very first program, so does the Unixoids family honor the beginning of their era.

At the same time, the idea is limited in time due to the implementation method. It can only be used until January 19, 2038. At 03:14:08 the number of seconds since 01/01/1970 will reach 2 ^ 31 (2147483648), which makes this number negative. It is said that using a non-32-bit and 64-bit variable to store time will allow the problem to be postponed for another 300 billion years: programmers like bugs as jokes, especially after 2000.

Period calculation and date determination

The time represented by a prime number makes it possible to have simple arithmetic.

The idea of using this arithmetic as record keys and / or unique events is interesting. Using the timestamp program in the PHP program with the user ID or visitor as the key, you do not have to worry about its uniqueness and do not waste time on it.

Inverse conversion to date and time

The time () function directly gives a numerical knowledge of the timestamp. Regardless of how and when the PHP date to timestamp conversion was performed, it can be brought back to any convenient representation by date (format [, timestamp]). If the second parameter is missing, the current date and / or time will be displayed. The value of format can be anything, language tools provide all the necessary options for localizing and formatting the date and time.

Storage in numerical form is always preferable to symbolic, and besides, the possibility of sorting is greatly simplified. It is also important to apply simple mathematics to control time periods and calculate dates.

It is hardly possible to assume an error in such calculations, the result of any calculations is interpreted in a natural date / time format is always correct.

Time localization

And although the PHP date to timestamp and the subsequent work with the number are convenient and ideal in the modern world, there are still a lot of reasons to store and use the date in its natural, and sometimes exclusively national format.

The power of modern computers is sufficient, databases effectively represent and store information, the sample is executed quickly, and the correct algorithm makes working with real dates quite acceptable.

Assembler times and byte savings have long been in the past. There are not so many tasks that require correct machine code and compact data representation, most of the projects are related to practical social spheres, where the date should be presented in a proper natural format.

With all this, by manipulating data in PHP, the timestamp does not lose its practical value and deserves to be in the arsenal of a qualified developer.

Current time

The general rule for all time functions is PHP: current time = timestamp. All dates specified manually by the user or the developer are usually data: the beginning or end of the period, the date of birth, hiring, etc. Temporary data is accepted through the interactive calendar. In particular, it is a datapicker from jQuery, allowing you to enter the date and / or time directly in the browser environment, on the site page.

Datapicker and similar developments are not only fully functional. They also provide the ability to customize localization as a date / time format, as well as names of months, days of the week, symbols for separating date and time elements.

The developer can execute on PHP date to timestamp for checking, comparing, performing arithmetic operations.

In this case, the data entered by the user (the visitor) is not a function of the PHP timestamp, the purpose of which is different. However, it is this specific feature of PHP that allows the developer to control the input of data on time and dates by the user.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.delachieve.com. Theme powered by WordPress.