Home > Backend Development > PHP Tutorial > Advanced application skills of PHP date and time functions_PHP tutorial

Advanced application skills of PHP date and time functions_PHP tutorial

WBOY
Release: 2016-07-21 15:44:04
Original
759 people have browsed it

PHP’s date and time function date()
1, year-month-day
echo date('Y-m-j');
2007-02-6
echo date ('y-n-j');
07-2-6
Uppercase Y represents the four-digit year, while lowercase y represents the two-digit year;
Lowercase m represents the number of the month (with leading) , and lowercase n represents the month number without a leading.
echo date('Y-M-j');
2007-Feb-6
echo date('Y-m-d');
2007-02-06
Capital M represents the month 3 abbreviation characters, and lowercase m represents the number of the month (with a leading 0);
There is no uppercase J, only a lowercase j represents the date of the month, without a leading o; if the month needs a leading, use a lowercase d.
echo date('Y-M-j');
2007-Feb-6
echo date('Y-F-jS');
2007-February-6th
Capital M represents the month 3 abbreviation characters, and capital F represents the full English version of the month. (No lowercase f)
Capital S represents the suffix of the date, such as "st", "nd", "rd" and "th", depending on the date number.
Summary:
can use uppercase Y and lowercase y to represent the year;
can use uppercase F, uppercase M, lowercase m and lowercase n to represent the month (two ways of representing characters and numbers respectively);
You can use lowercase d and lowercase j to represent the day, and uppercase S represents the suffix of the date.

2, hour:minute:second
By default, the time displayed by PHP interpretation is "Greenwich Mean Time", which is 8 hours different from our local time.
echo date('g:i:s a');
5:56:57 am
echo date('h:i:s A');
05:56:57 AM
A lowercase g indicates a 12-hour format without leading 0s, while a lowercase h indicates a 12-hour format with leading 0s.
When using the 12-hour clock, you need to indicate morning and afternoon. Lowercase a represents lowercase "am" and "pm", and uppercase A represents uppercase "AM" and "PM".
echo date('G:i:s');
14:02:26
Capital G represents the hour in 24-hour format, but without leading; use capital H to represent with leading 24-hour hour number
Summary:
The letter g represents the hour without a leading, the letter h represents the hour with a leading;
Lowercase g and h represent the 12-hour format, and uppercase G and H represent the 24-hour format.
3, leap year, week, day
echo date('L');
Whether this year is a leap year: 0
echo date('l');
Today Yes: Tuesday
echo date('D');
Today is: Tue
uppercase L means to determine whether this year is a leap year, Boolean value, if true, return 1, otherwise 0;
lowercase l means Today is the full English version of the day of the week (Tuesday);
uses a capital D to represent the 3-character abbreviation of the day of the week (Tue).
echo date('w');
Today's week: 2
echo date('W');
This week is the 06th week of the year
Lowercase w represents the day of the week , represented in numerical form
Capital W represents the number of weeks in the year
echo date('t');
This month has 28 days
echo date('z');
Today It is the 36th day of this year
Lowercase t indicates the number of days in the current month
Lowercase z indicates the day of the year today
4, others
echo date(' T');
UTC
Capital T indicates the server's time zone setting
echo date('I');
0
Capital I indicates whether it is daylight saving time, and returns if true 1, otherwise 0
echo date('U');
1170769424
Uppercase U represents the total number of seconds from January 1, 1970 to the present, which is the UNIX timestamp of the Unix time epoch.
echo date('c');
2007-02-06T14:24:43+00:00
Lowercase c represents ISO8601 date, the date format is YYYY-MM-DD, separated by the letter T Date and time, the time format is HH:MM:SS, and the time zone is expressed using the offset from Greenwich Mean Time (GMT).
echo date('r');
Tue, 06 Feb 2007 14:25:52 +0000
Lowercase r represents the RFC822 date.

The small date() function shows the power and charm of PHP. Let’s compare it to ASP, haha.
checkdate($month,$date,$year)
This function returns true if the applied value constitutes a valid date. For example, for the error date February 31, 2005, this function returns false.
This function can be used to check and validate a date before it is used in calculations or saved in the database.

Copy code The code is as follows:

// returns false
echo checkdate(2 ,30,2005) ? "valid" : "invalid";
// returns true
echo checkdate(4,6,2010) ? "valid" : "invalid";
?>

getdate($ts)
Without arguments, this function returns the current date and time by combining arrays. Each element in the array represents a specific component of the date/time value. An optional time stamp argument can be submitted to the function to obtain a date/time value corresponding to the time stamp.
Apply this function to obtain a series of discrete, easily separable date/time values.
Copy code The code is as follows:

// get date as associative array
$ arr = getdate();
echo "Date is " . $arr['mday'] . " " . $arr['weekday'] . " " . $arr['year'];
echo " Time is " . $arr['hours'] . ":" . $arr['minutes'];
?>

mktime($hour, $minute, $second, $month, $day, $year)
This function does the opposite of getdate(): it generates a UNIX from a series of date and time values Time tag (the number of seconds that have elapsed since January 1, 1970 GMT). When no arguments are used, it generates a UNIX time stamp of the current time.
Use this function to obtain the UNIX time label of the immediate time. Such timestamps are commonly used in many databases and programming languages.
Copy code The code is as follows:

// returns timestamp for 13:15:23 7 -Jun-2006
echo mktime(13,15,23,6,7,2006);
?>

date($format, $ts)
this This function formats a UNIX time stamp into a human-readable date string. It is the most powerful function in the PHP date/time API and can be used to convert integer time labels into the required string format in a series of correction values.
Apply this function when formatting time or date for display.
Copy code The code is as follows:

// format current date
// returns "13-Sep-2005 01:16 PM"
echo date("d-M-Y h:i A", mktime());
?>

strtotime($str)
This function converts a human-readable English date/time string into a UNIX time tag.
Apply this function to convert a non-standardized date/time string into a standard, compatible UNIX time tag.

Copy code The code is as follows:

// 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 stamp into a date string suitable for the current environment.
Apply this function to create a date string compatible with the current environment.

Copy code The code is as follows:

// set locale to France (on Windows)
setlocale(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()
As before As defined by the setlocale() function, this function formats a UNIX time stamp into a date string appropriate for the current environment.
Apply this function to create a date string compatible with the current environment.

Copy code The code is as follows:

// 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 consists of a series of GMT times The date and time value represented generates a UNIX time stamp. When no arguments are used, it generates a UNIX time stamp of the current GMT time.
Use this function to obtain the UNIX time label of GMT real-time time.

Copy code The code is as follows:

// returns timestamp for 12:25 :23 9-Jul-2006
echo gmmktime(12,25,23,7,9,2006);
?>



gmdate($ format, $ts)
This function formats a UNIX time stamp into a human-readable date string. This date string is expressed in GMT (not local time).
Apply this function when using GMT to represent the time label.

Copy code The code is as follows:

// 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), date_default_timezone_get()
All date/time function calls after this function set and restore the default time zone.
Note: This function is only valid in PHP 5.1+.
This function is a convenient shortcut for setting the time zone for future time operations.

Copy code The code is as follows:

// set timezone to UTC
date_default_timezone_set('UTC');
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320602.htmlTechArticlePHP’s date and time function date() 1, year-month-day echo date('Y-m-j') ; 2007-02-6 echo date('y-n-j'); 07-2-6 Uppercase Y represents the four-digit year, while lowercase y represents the two-digit year; Lowercase m represents...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template