PHP functions commonly used in project development

Source: Internet
Author: User
Tags echo date
Date Operation

To facilitate storage, comparison, and delivery, we often need to use the Strtotime () function to convert dates into Unix timestamps, and use the date () function to convert dates to the usual time format only when displayed to the user.

The strtotime () function resolves the datetime description of any English text to a Unix timestamp

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:

2006-01-30 17:48:24


String manipulation

Sometimes you need to get part of a string, you need to use the Intercept substr () function of the string

SUBSTR () function returns part of a string

Grammar:

SUBSTR (String,start,length)

eg

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

Output:

World


Array manipulation

Here are two very useful functions:

Array_unique () Moves the number of identical elements in the divisor group

When the values of several array elements are equal, only the first element is preserved, and the other elements are deleted.
The key name in the returned array is not changed.


Array_filter () to delete an empty element in the array

Grammar:

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

Each value in the input array is passed to the callback function in turn. 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 loop
Callback is the callback function that is used, and if no callback function is provided, all entries in input that are equivalent to FALSE (which can be used to delete elements that are empty in the array) are removed.

EG1:

 
  1, "B" =>2, "C" =>3, "D" =>4, "E" =>5); echo "ODD: \ n";p rint_r (Array_filter ($array 1, "ODD"));? >

Output:

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

EG2:

 
  ' foo ',             1 = False,             2 = 1,             3 = null,             4 = '          );p rint_r (Array_filter ($entry));? >

Output:

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

  • Related Article

    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.