Time stamps in PHP: timestamp and time calculation

Table of contents:

Time stamps in PHP: timestamp and time calculation
Time stamps in PHP: timestamp and time calculation
Anonim

Time always matters. A site that does not control time and does not use it continuously, not only does not have the right to life, but definitely does not belong to fully functional modern Internet resources.

PHP functions for working with time

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

PHP timestamp
PHP timestamp

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

PHP date to timestamp
PHP date to timestamp

In principle, two functions (getTimeStamp, GetDate) are enough to solve any task when working with date and/or time. But a sophisticated developer can use other features as well.

Important: in PHP, a timestamp is a kind of numeric stamp that gets a new one every secondnumeric meaning (value). Historically, this number is the number of seconds that have passed since midnight, January 1, 1970.

Objective reality and the meaning of Timestamp

There have always been many milestones in programming. As every decent programmer remembers the date of his very first program, so the Unixoid family honors the beginning of its era.

php time
php time

At the same time, the idea is limited in time due to the implementation method. You can use it only until January 19, 2038. At 03:14:08, the number of seconds since 1970-01-01 reaches 2^31 (2147483648), making this number negative. They say that using a 64-bit rather than a 32-bit variable to store the time will delay the problem for another 300 billion years: programmers love bugs like jokes, especially after 2000.

Calculating the period and determining the date

Time represented as a prime number makes it possible to have simple arithmetic.

simple arithmetic
simple arithmetic

It's an interesting idea to use this arithmetic as keys for records and/or unique events. Using a timestamp in a PHP program in combination with a user or visitor ID as a key, you don't have to worry about its uniqueness and don't waste time on it.

Reverse conversion to date and time

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

Time Format
Time Format

Storage in numerical form is always preferable to symbolic, and besides, sorting is greatly simplified. Also important is the ability to apply simple math to control time periods and calculate dates.

It is hardly possible to make a mistake in such calculations, the result of any calculations is always correctly interpreted in the natural date/time format.

Time localization

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

PHP current time timestamp
PHP current time timestamp

Modern computers are powerful enough, databases efficiently represent and store information, sampling is fast, and the right algorithm makes working with real dates quite acceptable.

The days of assembler and saving on bytes are long gone. There are not many tasks that require correct machine code and compact data representation, most of the projects are related to practical social areas where the date must be represented in the proper natural format.

With all this, manipulating datain PHP, the timestamp has not lost its practical value and deserves to be in the arsenal of a qualified developer.

Current time

General rule for all PHP time functions: current time=timestamp. All dates set manually by the user or developer, as a rule, are data: the beginning or end of the period, date of birth, employment, etc. It is customary to enter temporary data through an interactive calendar. In particular, this is a jQuery datapicker that allows you to enter the date and / or time directly in the browser environment, on the site page.

Datepicker
Datepicker

Datapicker and similar developments are not only fully functional. They also provide customizable localization for both the date/time format and the names of the months, days of the week, separators for date and time elements.

A developer can execute date to timestamp in PHP to check, compare, perform arithmetic operations.

At the same time, the data entered by the user (visitor) is not the PHP timestamp function, the purpose of which is different. However, it is this specificity of PHP functions that allows the developer to control the input of time and date data by the user.

Popular topic

Editor's choice