Common PHP function _ PHP tutorials in project development

Source: Internet
Author: User
Tags echo date
Common PHP functions in project development. In order to facilitate storage, comparison, and transmission, we usually need to use the strtotime () function to convert a date to a UNIX timestamp, only PHP functions commonly used in project development are displayed.

Date operation

To facilitate storage, comparison, and transmission, we usually need to use the strtotime () function to convert a date to a UNIX Timestamp. date () is used only when it is displayed to users () the function converts a date to a common time format.

The strtotime () function parses the date and time descriptions of any English text into Unix timestamps.

Eg:

 

Output:

1138614504
1128290400
1138632504
1139219304
1139503709
1139180400
1138489200


The date () function converts a timestamp to a common date format.

Eg:

Echo date ('Y-m-d H: I: S', "1138614504 ");

Output:

17:48:24


String operation

Sometimes a part of a string needs to be obtained, and the substr () function of the string must be used.

The substr () function returns part of the string.

Syntax:

Substr (string, start, length)

Eg:

Echo substr ("Hello world! ", 6, 5 );

Output:

World


Array Operations

Here we will introduce two very useful functions:

Array_unique () removes the number of identical elements from the array.

When the values of several array elements are equal, only the first element is retained, and other elements are deleted.
The input key of the returned array remains unchanged.


Array_filter () deletes an element that is null in the array.

Syntax:

Array array_filter (array $ input [, callable $ callback = ""])

Pass each value in the input array to the callback function in sequence. If the callback function returns TRUE, the current value of the input array is included in the returned result array. The key name of the array remains unchanged.

Input is the array to be recycled
Callback is the used callback function. if the callback function is not provided, all the entries in the input that are equivalent to FALSE will be deleted (you can delete the element empty in the array ).

Eg1:

 1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);echo "Odd :\n";print_r(array_filter($array1, "odd"));?>

Output:

Odd:
Array
(
[A] => 1
[C] => 3
[E] => 5
)

Eg2:

  'foo',             1 => false,             2 => -1,             3 => null,             4 => ''          );print_r(array_filter($entry));?>

Output:

Array
(
[0] => foo
[2] =>-1
)


In order to facilitate storage, comparison, and transmission, we usually need to use the strtotime () function to convert a date to a UNIX timestamp, only for display to use...

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.