PHP’s date and time function date()
1, year-month-day
Copy code The code is as follows:
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), while lowercase n represents Month number without leading.
Copy code The code is as follows:
echo date('Y-M-j');
2007-Feb-6
echo date('Y-m-d');
2007-02-06
Capital M represents the 3 abbreviation characters of the month, while lowercase m represents the month Numbers (with leading 0);
There is no uppercase J, only lowercase j represents the date of the month, without leading o; if the month needs to be preceded, use lowercase d.
Copy code The code is as follows:
echo date('Y-M-j');
2007-Feb-6
echo date('Y-F-jS');
2007-February-6th
Capital M represents the 3 abbreviation characters of the month, while capital F represents the English of the month Write in full. (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 to represent characters and numbers respectively) );
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.
Copy code The code is as follows:
echo date('g:i:s a');
5: 56:57 am
echo date('h:i:s A');
05:56:57 AM
lowercase g indicates 12-hour format, without leading 0, while a lowercase h indicates a 12-hour clock 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".
Copy code The code is as follows:
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 the hour in 24-hour format with leading
Summary:
The letter g represents the hour without a leading, and the letter h represents the hour with a leading;
Lowercase g and h represent the 12-hour format, and capital G and H represent the 24-hour format.
3, leap year, week, day
Copy code The code is as follows:
echo date('L ');
Whether this year is a leap year: 0
echo date('l');
Today is: Tuesday
echo date('D');
Today Yes: Tue
Capital L indicates whether this year is a leap year, Boolean value, returns 1 if true, otherwise 0;
Lowercase l indicates the full English version of the day of the week (Tuesday);
Use a capital D to represent the 3-character abbreviation of the day of the week (Tue).
Copy code The code is as follows:
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, and the number form represents
uppercase W Represents the number of weeks in the year
Copy code The code is as follows:
echo date('t');
This month is 28 days
echo date('z');
Today 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 represents the server's time locale
echo date('I');
0
Capital I represents Determine whether the current daylight saving time is, return 1 if true, otherwise 0
echo date('U');
1170769424
uppercase U represents the total number of days from January 1, 1970 to the present The number of seconds is the UNIX timestamp of the Unix time epoch.
echo date('c');
2007-02-06T14:24:43+00:00
Lowercase c represents the ISO8601 date, the date format is YYYY-MM-DD, using letters T to separate the date and time, the time format is HH:MM:SS, and the time zone is represented by 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.
PHP date time function date time: How to display the current system time and synchronize the clock
Tell you a simple method:
The result is: 2009-10-06 16:21:38
time() function: displays the current time of the system.
date() function: Format the server's time.
Syntax: string date(string format, int [timestamp]);
Function type: Time and date
Return value: String
The parameters are as follows:
a - "am" or "pm"
A - "AM" or "PM"
d - day, two digits, if there are less than two digits, add zero in front; for example: "01" to "31" "
D - day of the week, three English letters; such as: "Fri"
F - month, full English name; such as: "January"
h - hour in 12-hour format; such as: "01 " to "12"
H - hours in 24-hour format; such as: "00" to "23"
g - hours in 12-hour format, no zeros are added for less than two digits; such as: "1" to 12 "
G - hour in 24-hour format, no zeros are added for less than two digits; such as: "0" to "23"
i - minutes; such as: "00" to "59"
j - number Day, two digits, if there are less than two digits, do not add zeros; For example: "1" to "31"
l - Day of the week, full English name; For example: "Friday"
m - Month, two digits , if there are less than two digits, add zeros in front; for example: "01" to "12"
n - month, two digits, if there are less than two digits, do not add zeros; for example: "1" to "12"
M - month, three English letters; such as: "Jan"
s - seconds; such as: "00" to "59"
S - add an English ordinal at the end of the word, two English letters; such as: "th", "nd"
t - the number of days in the specified month; such as: "28" to "31"
U - the total number of seconds
w - the numeric day of the week, such as: "0" (Sunday) to "6" (Saturday)
Y - year, four digits; such as: "1999"
y - year, two digits; such as: "99"
z - year The number of days; such as: "0" to "365"
If the displayed time is inconsistent with the system, you need to change the PHP.ini configuration file
The system default is UTC time
You can open php.inc.
Set date.timezone = PRC
Advanced application skills of PHP date and time functions
The PHP date and time function is introduced in date() Simple usage, this class will introduce more functions to enrich our application
checkdate($month,$date,$year)
If the applied value constitutes a valid date, then the function returns true. 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 in a combined array. 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 has the opposite effect of getdate(): it consists of a A series of date and time values generates a UNIX time stamp (the number of seconds 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 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 stamp.
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 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:
// 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 stamp from a series of date and time values expressed in GMT time. 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).
This function is applied when the time label is expressed in GMT.
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');
?>
http://www.bkjia.com/PHPjc/319050.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319050.htmlTechArticlePHP’s date and time function date() 1, year-month-day copy code code is as follows: 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 year...