Use PHP's date and time functions to improve development skills

Source: Internet
Author: User
Tags current time execution getdate local time locale php and string format valid

Whether they are accustomed to displaying the current server time or logging the next instance of a database entry, PHP's date and time functions are an important part of their toolkit for any network developer.

But any tool needs a person to dominate to play the advantage.

That's why I wrote this article. The following table (table A) lists some of the most important date and time functions in PHP and provides clear instructions for using them.

Keep reading, you will learn something new!

Table A

Function

Description

Instance

Checkdate

($month,

? date, $year)

If the value that is applied constitutes a valid date, the function returns True. For example, for an error date of February 31, 2005, this function returns to False.

You can use this function to check the date and make the date effective before the date is calculated or saved in the database.

<?php

Returns false

echo checkdate (2,30,2005)?

"Valid":

"Invalid";

Returns True

echo checkdate (4,6,2010)? "Valid":

"Invalid";

?>

GETDATE ($ts)

In the absence of an argument, the function returns the current date and time in a associative array. Each element in the array represents a specific component in the date/time value. You can submit an optional time label argument to a function to get the date/time value that corresponds to the time label.

This function is used to obtain a series of discrete, easily separated date/time values.

<?php

Returns timestamp for

13:15:23

7-jun-2006

echo Mktime (13,15,23,6,7,2006);

?>

Mktime ($hour,

$minute,

$second,

$month, $day,

$year)

The function is the opposite of getdate (): It generates a UNIX time label from a series of date and time values (the number of seconds from GMT January 1, 1970 to the current elapsed). When the argument is not used, it generates the UNIX time label for the current time.

Use this function to get the UNIX time label for the instant time. This time tag is commonly used in many database and program languages.

<?php

Returns timestamp for

13:15:23

7-jun-2006

echo Mktime (13,15,23,6,7,2006);

?>

Date ($format,

$TS)

This function formats the UNIX time label as a date string that can be read artificially. It is the most powerful function in the PHP date/Time API, and can be used in a series of correction values to convert the integer time label to the desired string format.

This function is applied to display the formatting time or date.

<?php

Format Current Date

Returns "13-sep-2005 01:16 PM"

echo Date ("D-m-y h:i A", mktime

());

?>

Strtotime

($STR)

This function converts an English date/time string that can be artificially read into a UNIX time label.

Apply this function to convert an nonstandard date/time string into a standard, compatible UNIX time label.

<?php

Returns 13-sep-05

echo Date ("D-m-y",

Strtotime ("Today"));

Returns 14-sep-05

echo Date ("D-m-y",

Strtotime ("Tomorrow"));

Returns 16-sep-05

echo Date ("D-m-y", Strtotime ("Today +3

Days "));

?>

strftime

($format,

? ts)

As defined by the previous setlocale () function, this function formats the UNIX time label as a date string that applies to the current environment. The

Applies this function to establish a date string that is compatible with the current environment.

<?php

//Set Locale to France (on

Windows)

Setl Ocale (Lc_time, "Fra_fra");

//Format month/day names

//As per locale setting

//returns "Septembre" and

Mardi

Echo strftime ("Month:%B");

Echo strftime ("Day:%A");

?

Microtime ()

This function returns the number of seconds and microseconds elapsed from the GMT time of January 1, 1970 to now. The

Applies this function when a datum-specific block of code is used to accurately measure its execution time.

<?php

//Get starting value

$start = Microtime ();

//Run some code

for ($x =0 $x <1000; $x + +) {

牋? $null = $x * $x;

}

//Get ending value

$end = Microtime ();

//Calculate time taken for

Code

Execution

Echo "Elapsed time:". ($end-

$start). " SEC ";

?

Gmmktime

($hour,

$minute,

$second,

$month, $day,

$year)

This function generates a UNIX time label from a series of date and time values expressed in GMT time. When you do not use a variable, it generates a UNIX time label for the current GMT instant time.

Use this function to obtain the UNIX time label for GMT Instant time.

<?php

Returns timestamp for

12:25:23

9-jul-2006

Echo Gmmktime

(12,25,23,7,9,2006);

?>

Gmdate

($format,

$TS)

This function formats the UNIX time label as a date string that can be read artificially. This date string is represented as GMT (not local time).

This function is applied when the time label is represented by GMT.

<?php

Format current date into GMT

Returns "13-sep-2005 08:32

AM "

Echo gmdate ("D-m-y h:i A",

Mktime ());

?>

Date_default_

Timezone_set

($tz) and

Date_default_

Timezone_get ()

This function then all the date/time function calls set and restores the default time zone.

Note: This function is only valid in PHP 5.1+.

This function is a convenient shortcut for setting a time zone for future time operations.

<?php

Set TimeZone to UTC

Date_default_timezone_set

(' UTC ');

?>

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.