PHP functions commonly used in project development

Source: Internet
Author: User
Tags date format array echo date empty functions key string

Date action

For ease of storage, comparison, and delivery, we typically use the Strtotime () function to convert a date to a UNIX timestamp and use the date () function to convert the date to the usual time format only when displayed to the user.

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

eg

 
  

Output:

1138614504
1128290400
1138632504
1139219304
1139503709
1139180400
1138489200


The date () function converts the timestamp to the usual 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 a part of a string and you need to use the Intercept substr () function of the string

The substr () function returns part of the string

Grammar:

SUBSTR (String,start,length)

eg

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

Output:

World


Array operations

Here are two very useful functions:

Array_unique () to move 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 unchanged.


Array_filter () to delete an empty element in an array

Grammar:

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

In turn, each value in the input array is passed to the callback function. 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 an array to loop
Callback for the callback function used, if no callback function is provided, all entries in input that are equivalent to FALSE are deleted (you can use this to delete an element that is empty in the array).

EG1:

 
  1, "B" =>2, "C" =>3, "D" =>4, "E" =>5);
echo "ODD: \ n";
Print_r (Array_filter ($array 1, "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
)




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.