intval() PHP function: Convert to integer. Manipulating PHP Data Types

Integer numbers do not have priority over real numbers, but they are destined for a largely “non-digital” purpose. An exact number with a specific number of digits and/or in a specific range of values ​​makes sense for forming CSS rules, can be used as an array key, or unique code database table row records. The integer can be part of a unique code, a password, or checksum when transferring data. An integer is far from being a matter of mathematics and numerical methods; it is often an element of a data or its code.

The meaning of the intval() function

The intval() PHP function is used to convert a “variable to an integer” and has two parameters and a double meaning.

The second parameter is the base of the required number system. Default is decimal system. If you do not specify the second parameter, then a number that begins with the character “0” is considered octal, and those starting with the character “0x” are considered hexadecimal.

The second parameter has a value if the first parameter is a character string.

The last two examples (very large values) show that the result is inappropriate. Essentially, what matters here is the bit capacity of the machine ( operating system). In practice, conversions of such numbers do not make much sense.

The logic behind the PHP intval function is to convert a string or number to an integer for practical application. For example, form CSS rule, which uses only integers. Convert a real number to an integer by simply removing only the integer part.

Using the intval() function

The intval() function in PHP is not the only one for the purpose of getting an integer. You can use round(), ceil() and floor(). These functions operate with generally accepted rounding logic.

The intval() PHP function is devoid of any logic and acts on the principle of getting an integer. If required, then for the right reason the required system Reckoning.

The given examples of intval() PHP show that its main area of ​​​​application is the logic of the algorithm, and not the logic of calculations.

Among various programming tasks, various manipulations with date and time values ​​are quite common. Rare automated system, the database can do without storing information about the time of a particular process. In addition to the simplest addition of a date to a database record or displaying this date, there are many tasks of displaying these dates in in various forms, checking whether the current time matches set timer, calculating the period between two dates and much more.

To make working with dates easier, each programming language has its own special data types for storing date and time values. Most often this is a numeric value, either an integer or a floating point value.

IN PHP work with a date, it most often encounters UNIX TIMESTAMP. Here the time is stored as an integer. The calculation of time begins on January 1, 1970. Therefore, for example, the date and time 12/11/2014 19:40:00 would be represented by the number 1418316000. This number shows how many seconds have passed since the zero date of January 1, 1970, called the Unix Epoch.

An example of a PHP page that provides data conversion capabilities is presented on the website in the programs section with the program "Convert date and time format". Here you can generate the desired date in the UNIX TIMESTAMP format, as well as bring this format into a standard, human-readable form.

Getting current time and date in PHP

To get the current server time, use the function

int time(void)

which will return the value in unix timestamp format.

echo time(); // output the current time in unix timestamp format

At first glance not very convenient format for a person, but, as is known, than simpler presentation data, the faster the computer processes these values. In addition, storing a number in a database is much more cost-effective than using any special format. Also, PHP works the same over time on both Unix and Windows platform, which makes it possible to use the code on any of these platforms.

Convert date and time format in PHP

The simplest mechanism for converting a numeric date value into more understandable values ​​is provided by the function:

array getdate()

She returns associative array, containing date information. If the timestamp parameter is not specified, the current time will be returned. This array contains the following values:

The resulting array allows you to display the values ​​in the desired form:

$date = 1418372345;
$date_mas = getdate($date);
echo $date_mas["mday" ] . " . " . $date_mas["mon" ] . " . " . $date_mas["year" ]; // 12.12.2014


You can also use the function to convert the date and time format:

string date(string $template [, int $unix_timestamp])

It is designed to get the current unix timestamp date in in the required format. The $template string parameter specifies the output format. The $unix_timestamp parameter can be used to specify which time value to work with. It is optional, so if it is not specified it will be used current date and time.

The format is specified by the following values:

a"before" and "after" noon: "am" or "pm"
A"before" and "after" noon in capital letters: "AM" or "PM"
dday of the month in 2 digits (if less than 10, zero comes first) (01 to 31)
Dday of the week in 3 letters. For example, "Mon" (Monday)
jday of the month, 1-2 digits without leading zeros (from 1 to 31)
Fname of the month. For example, "January"
hhour, 12-hour format (01 to 12)
Hhour, 24-hour format (00 to 23)
ghour, 12-hour format without zeros (1 to 12)
Ghour, 24-hour format without zeros (0 to 23)
iminutes (00 to 59)
I (capital i)1 if daylight saving time is in effect, otherwise 0
L1 if the year is a leap year, or 0 if it is not a leap year
Btime in Internet time format ( alternative system time of day countdown) (from 000 to 999)
Tcomputer time zone. For example, MDT
l (lowercase L)day of the week. For example, "Monday"
mmonth, two digits with zeros (01 to 12)
nmonth, one or two digits without zeros (from 1 to 12)
Mabbreviated name of the month. For example, "Jan"
tnumber of days in the specified month (from 28 to 31)
sseconds (0 to 59)
SEnglish two-letter ordinal number suffix ("st", "nd", "rd" or "th")
Uinteger number of seconds since the start of the UNIX epoch
yyear, digital, 2 digits (14)
Yyear, digital, 4 digits (2014)
zordinal number of the day in the year (from 0 to 365)
Ztime zone offset in seconds (from -43200 to 43200)
Nserial number days of the week from 1 (Monday) to 7 (Sunday) in accordance with the ISO-8601 standard, (added to PHP versions 5.1.0)
wserial number of the day of the week from 0 (Sunday) to 6 (Saturday)
Wserial number of the week of the year in accordance with the ISO-8601 standard; weeks start on Monday (added in PHP 4.1.0)
oyear number according to ISO-8601 standard. Has the same meaning as Y, except when the ISO week number (W) is from the previous or following year; then the year of that week will be used. (added in PHP 5.1.0)
etime zone scale code. For example: UTC, GMT, Atlantic/Azores (added in PHP 5.1.0)
Odifference with Greenwich time, in hours. For example: +0200
Pdifference from Greenwich Mean Time with a colon between hours and minutes. For example: +02:00 (added in PHP 5.1.3)
cdate in format ISO standard 8601. For example, 2014-12-12T15:19:21+00:00 (added in PHP 5)
rdate in » RFC 2822 format. For example: Thu, 21 Dec 2000 16:01:07 +0200
Unumber of seconds that have passed since the beginning of the Unix Epoch (The Unix Epoch, January 1, 1970 00:00:00 GMT)

As you can see from the list, you can get a lot of useful date data using this function. For example:

$date = 1418372345; // initial date and time 12/12/2014 11:19:05

echo date("d.m.Y" , $date); // 12.12.2014 (date)
echo date("H:i:s" , $date); // 11:19:05 (time)
echo date("H:i" , $date); // 11:19 (time)
echo date("t" , $date); // 31 (number of days in a month)
echo date("z" , $date); // 345 (ordinal number of the day in the year)
echo date("l dS \of F Y h:i:s A" , $date); // Friday 12th of December 2014 11:19:05 AM

Other characters included in the pattern will be printed as is on the line. If you need to enter a character that is used in a function as a format code, the "\" character is inserted in front of them. For the value "\n" (the transition character new line), you should specify "\\n". Thus, you can output an entire message containing date and time information:

echo date( "Today z-th day Y year", $date); // Today is the 345th day of 2014

Convert date and time to timestamp format

To reverse date conversion from standard format The function is applied to the numeric timestamp value:

int mktime(]]]]]])

The mktime() function returns the Unix time value corresponding to the date and time specified by its arguments. For example:

$my_date = mktime(10, 30, 0, 12, 12, 2014);

You should be careful about the order of the function arguments: hours, minutes, seconds, month, day, year.

In addition to simply generating a date value into a timestamp, the mktime() function can be used to perform arithmetically with dates. To do this you can simply enter necessary arguments. For example, if you specify the 14th month, then in the final value, the month will be the 2nd, and the year value will increase by one:

$my_day = 12;
$my_month = 12;
$my_year = 2014;

$new_date = mktime(0, 0, 0, $my_month + 5, $my_day, $my_year);

echo date("d.m.Y" , $new_date); // 05/12/2015 (date)

You can do the same with other parameters.

Checking date validity in PHP

When working with dates, especially when generating a date using the mktime() function proposed above, it is necessary to take into account the correctness of the entered date. To do this, PHP uses the function:

bool checkdate(int month, int day, int year)

Returns true if the date specified by the arguments is correct; otherwise it returns false. The date is considered correct if:

Year in the range from 1 to 32767;

Month ranges from 1 to 12;

The day for a given month, taking into account leap years, is indicated correctly.

Date checking example:

$my_day = 32;
$my_month = 12;
$my_year = 2014;

if (!checkdate($my_month, $my_day, $my_year))
echo "Error: the date is incorrect";

Information has always been represented by symbols. Only when programming appeared did it partially become letters, numbers and signs (in the formal sense).

Until a certain time, the most popular were oral speech, hand-made symbols, gestures, as well as works of art, in particular. It is important that all these options for the natural presentation of information, from small to large, could always be described in words, that is, made accessible software processing in one sense or another.

Strings and numbers

Strings are, first of all, collections various characters. In different programming tools fractional part separated from the whole in different ways. Some preferred the period, others the comma (in PHP conversion strings to number implies "."). Triads were separated by a single apostrophe, comma, or space. Real numbers were written with the letter "E".

All these are formalities. Mathematics and the principles of operation of any processor have always recognized numbers without frills, and corresponding additional processors have been developed to work with real values.

But the world of variables of a specific type (for early-era programs) and the implementation of the modern idea of ​​consensus of all data types means: in PHP, converting a string to a number is the same as doing nothing. IN best case scenario PHP will “cut off”, or more precisely, make it 0 or a number, if there is at least one digit in the source string. All digits up to the first non-digit character will add up to this number.

General conversion rule

In PHP, the conversion of a string to a number, unless the programmer has specified otherwise, occurs automatically at the time of application. If a variable contains only numbers and a period, then a real number can be obtained. If a comma is encountered in a sequence of numbers, then everything that comes before it will be interpreted as a number, naturally, an integer.

Any non-numeric character in the sequence of numbers stops the process, and the result of PHP's string-to-number conversion will be only the value that formed before the first non-numeric character.

Mathematics and symbol processing

What is more difficult, the first or the second, even from the standpoint of formulas that do not fit on the page, impressive matrices, graphs that sometimes even the authors cannot untangle, derivatives of integrals, differentials of integrals and polynomials in the nth generation, it is very simple to say: society , of course, honors the merits of mathematics and mathematicians, but the processing of symbols is more important, more complex and covers mathematics like a duvet, providing an inquisitive mind that cannot go beyond the magic of numbers, a favorable environment for creativity.

Usually on PHP string is converted to a number automatically, but it's never a bad idea to check this. It's best to always be sure that PHP correctly understands that a given variable is a number. If there is even a shadow of doubt, you should explicitly specify the type of the variable (int) or (float) before the value and check how PHP understands the type variable functions is_integer() or is_float().

Modern programming trends

The classic interpretation of the PHP "string to number" problem lies within the representations modern programming. The reverse process has much more varied options. Functions number_format(), printf(), sprintf() can work wonders, not only at the level of a single number, but also at a group of numbers.

Conversion to PHP" line to number" indicates, rather, that we can do this too, but m mathematics and numerical calculations are not our strong point.

Writing decent and reliable functionality that implements this or that number processing in PHP is not a problem, and the result will not be any worse than the result from more mathematical languages. PHP is, first of all, a server language, a language for processing information, including mathematical information.

I have an application that touches clients all over the world, and naturally I want everything that goes into my databases to be UTF-8 encoded.

The main problem for me is that I don't know what the source encoding of any string will be - it could be from text field(usage

only useful if the user actually submitted the form) or it could be from a loaded text file, so I really can't control the input.

I need a function or class that ensures that the stuff coming into my database is, as far as possible, UTF-8 encoded. I tried iconv(mb_detect_encoding($text), "UTF-8", $text); but this has problems (if the input is "fiancée", it returns "fianc"). I tried many things =/

For uploading files I like the idea of ​​asking end user indicate the encoding they are using and show them preview of what the output will look like, but that doesn't help against nasty hackers (it might actually make their life a little easier).

I've read other SO questions on this subject, but they seem to have subtle differences such as "I need to parse RSS feeds" or "I remove data from websites" (or, indeed, "You can't") .

But there must be something that at least, has a nice try!

9 answers

What you are asking is extremely difficult. If possible, it is best to set the encoding to the user. Preventing an attack doesn't have to be much easier or more difficult.

However you can try:

Iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);

Setting a strict value can help you get a better result.

In our homeland of Russia we have 4 popular encodings, so your question is very popular here.

With char character codes alone you can't detect the encoding because the code pages overlap. Some code pages on different languages even have complete intersection. So, we need a different approach.

The only way to work with unknown encodings is to work with probabilities. So we don't want to answer the question "what is the encoding of this text?", we are trying to understand " what is most likely the encoding of this text?".

One guy from a popular Russian technology blog came up with this approach:

Create a probability range of char codes in each encoding you want to support. You can build it using some large texts in your language (eg some fiction, use Shakespeare for English and Tolstoy for Russian, lol). You will get something like this:

Encoding_1: 190 => 0.095249209893009, 222 => 0.095249209893009, ... encoding_2: 239 => 0.095249209893009, 207 => 0.095249209893009, ... encoding_N: => probabilty

Next. You take text in an unknown encoding and for each encoding in your "probability dictionary" you look up the frequency of each character in the text with the unknown encoding. Total probability of symbols. Coding with high rating, is probably the winner. Best results for large texts.

If you are interested, I can happily help you solve this problem. We can significantly improve accuracy by creating a two-character list of probabilities.

Btw. mb_detect_encoding certanly doesn't work. Yes, in general. Please have a look source code mb_detect_encoding in "ext/mbstring/libmbfl/mbfl/mbfl_ident.c".

You've probably tried this, but why not just use the mb_convert_encoding function? It will try to automatically determine the char set of the provided text, or you can pass it a list.

Additionally, I tried running:

$text = "fiancée"; echo mb_convert_encoding($text, "UTF-8"); echo "

"; echo iconv(mb_detect_encoding($text), "UTF-8", $text);

and the results are the same for both. How do you see your text being truncated to "fianc"? is it in the database or in the browser?

It is impossible to identify a string encoding that is completely accurate. There are ways to try to guess the encoding. One of these methods and probably/currently the best in PHP is mb_detect_encoding(). This scans your string and looks for occurrences of things unique to certain encodings. Depending on your string there may not be such distinguishable cases.

Take ISO-8859-1 and ISO-8859-15 encoding (http://en.wikipedia.org/wiki/ISO/IEC_8859-15#Changes_from_ISO-8859-1)

There are only a few different characters, and to make them worse, they were represented by the same bytes. It is impossible to discover, given a string without knowing its encoding, whether byte 0xA4 should represent ¤ or € in your string, so there is no way to know its exact encoding.

(Note: you could add a human element or an even more advanced scanning method (like what Oroboros102 suggests) to try to figure out, based on the surrounding context, if the character should be ¤ or €, although that seems like a bridge too far)

There are more distinguishable differences between, for example, UTF-8 and ISO-8859-1, so it's still worth trying to figure it out when you're not sure, although you can and shouldn't rely on it being correct.

There are other ways to ensure correct encoding. As for forms, try to force UTF-8 as much as possible (check Snowman to make sure you will represent UTF-8 in every browser: http://intertwingly.net/blog/2010/07/29/Rails-and-Snowmen) This is done so at least you can be sure that every text submitted through your forms is utf_8. For downloaded files, try running the unix command "file -i" on it, for example. exec() (if possible on your server) to aid discovery (using the document specification). As for scraping data, you can read the HTTP headers, which usually determine the encoding. When analyzing XML files see If the XML metadata contains a character set definition.

Instead of trying to automatically guess the encoding, you should first try to provide the specific encoding yourself if possible, or try to get the definition from the source you get it from (if applicable) before resorting to detection.

The main problem for me is that I don't know what the source encoding of any string will be - it could be from a text field (only useful if the user has actually submitted the form), or it could be from a loaded text file, so I really can't control the input.

I don't think this is a problem. The application knows the input source. If it's from a form, use UTF-8 encoding in your case. It works. Just make sure the data provided is encoded correctly (check). Keep in mind that not all databases fully support UTF-8.

If it's a file, you won't save it in UTF-8 encoding in the database, but in binary form. When you output the file again, use binary output too, then it's completely transparent.

Your idea is good that the user can tell the encoding if he/she can tell after downloading the file anyway since it is binary.

So I have to admit that I don't see specific problem which you raise with your question. But maybe you can add some details about your problem.

Grim, there are some really good answers and attempts to answer your question here. I would like to thank everyone for their responses. They are great. I'm not a coding master, but I understand your desire to have a pure UTF-8 stack in your database. I'm using MySQL utf8mb4 for tables, fields and connections.

My situation boiled down to "I just want my sanitizers, validators, business logic and prepared statements to handle UTF-8 when the data comes from HTML forms or links to register by email". So, I think I started with this idea:

  • Trying to detect encoding: $encodings = ["UTF-8", "ISO-8859-1", "ASCII"];
  • If the encoding cannot be detected, throw new RuntimeException
  • If the input is UTF-8, continue.
  • Otherwise if it is ISO-8859-1 or ASCII

    A. Attempting to convert to UTF-8 (pending, not completed)

    b. Determine the encoding of the converted value

    With. If the reported encoded and converted value is UTF-8, proceed.

    e. Else, throw new RuntimeException

From mine abstract class Sanitizer

Private function isUTF8($encoding, $value) ( ​​return (($encoding === "UTF-8") && (utf8_encode(utf8_decode($value)) === $value)); ) private function utf8tify(&$ value) ( ​​$encodings = ["UTF-8", "ISO-8859-1", "ASCII"]; mb_internal_encoding("UTF-8"); mb_substitute_character(0xfffd); //REPLACEMENT CHARACTER mb_detect_order($encodings); $stringEncoding = mb_detect_encoding($value, $encodings, true); if (!$stringEncoding) ( $value = null; throw new \RuntimeException("Unable to identify character encoding in sanitizer."); ) if ($this->isUTF8($stringEncoding, $value)) ( return; ) else ( $value = mb_convert_encoding($value, "UTF-8", $stringEncoding); $stringEncoding = mb_detect_encoding ($value, $encodings, true); if ($this->isUTF8($stringEncoding, $value)) ( return; ) else ( $value = null; throw new \RuntimeException("Unable to convert character encoding from ISO- 8859-1, or ASCII, to UTF-8 in sanitizer."); ) ) return; )

An argument could be made that I should decouple the encoding concerns from my abstract Sanitizer class and simply insert an Encoder object into a concrete child Sanitizer instance. However, the main problem with my approach is that I, without any knowledge, reject coding types that I don't need (and I rely on PHP functions mb_ *). Without further study, I can't know if some people are hurt or not (or if I'm losing important information). So I need to find out more. I found this article.