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

Source: Internet
Author: User
Tags date array current time execution getdate 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)

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";

&NBSP;

//returns True

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

"invalid";

?>

getdate ($ts)

applies this function to obtain a series of discrete, easily separated date/time values.

<?php

// Timestamp for

13:15:23

7-jun-2006

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

?>

mktime ($hour,

$month, $day,

$year)

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. The

uses 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

// Timestamp for

13:15:23

7-jun-2006

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

?>

date ($format,

$ts)

 

to display the formatted 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)

applies this function to convert non-standard date/time strings into standard, compatible Unix time labels.

<?php

//returns 13- Sep-05

echo date ("D-m-y",

strtotime ("Today"));

&NBSP;

//returns 14-sep-05

echo date ("D-m-y",

strtotime ("Tomorrow"));

&NBSP;

//returns 16-sep-05

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

days"));

?>

strftime

($format,

?ts)

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

<?php

//set locale to France (on

windows)

setlocale (Lc_time, "Fra_fra");

&NBSP;

//format month/day names

//as per locale setting

//returns "septembre" and

"mardi"

echo strftime ("Month:%B");

echo strftime ("Day:%A");

?>

microtime ()

< P class= "Style2" This function returns the number of seconds and microseconds elapsed between GMT time January 1, 1970 and now. The

applies this function when a datum-specific block of code accurately measures its execution time.

<?php

//get starting Value

$start = Microtime ();

&NBSP;

//run some code

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

牋? $null = $x * $x;

}

&NBSP;

//get ending value

$ End = Microtime ();

&NBSP;

//calculate time taken for

code

execution

echo "Elapsed Time:". ($end-

&NBSP;

$start). " SEC ";

?>

gmmktime

($hour,

$minute,

$second,

$month, $day,

$year)

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

<?php

// Timestamp for

12:25:23

9-jul-2006

echo

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

?>

gmdate

($format,

$ts)

applies this function when it represents a time label with 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 valid only 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 ');

?>



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.