PHP has many easy-to-use functions to display and process dates.
To display a date or time in a specific format, you can use the date () function. It has two parameters: how to display the date format and the timestamp representing the date you want to display. This timestamp must be the total number of seconds from January 1, 1970 (if you want to use the current time, you can use the time () function, this function returns the "current" timestamp ). Date () has many format options, just like the strftime () function in C or the POSIX: strftime () function in Perl.
<? Php
$ Birthday_stamp = mktime );
$ Birthday_formatted = date ('f d, Y-g: ia ', $ birthday_stamp );
Echo "David was born on $ birthday_formatted ."
?>
Will display
David was born on March 10,197 5--7: 45 p. m.
Of course, if you need a specific known date, this complex format function is not very useful. Because you already know what your format will be. These functions are useful when processing the form output part that requires the user to select a date:
<Select name = "when">
<? Php
$ D = time ();
For ($ I = 0; $ I <10; $ I ++ ){
Echo '<option value = "'. $ d. '">'. date ('f d', $ d );
$ D + = 86400;
}
?>
</SELECT>
The above will output a single region, with ten options-today and the next nine days. Before the program loop starts, we store the current time in the variable $ d. Each <OPTION> value is displayed, and the value is calculated using a Unix timestamp, the displayed text is set to month and day ("July 27", "July 28", and so on ). After the value is displayed, the variable $ d is added with 86,400 (the total number of seconds of the 24 hours a day-24 hours * 60 minutes * 60 seconds ).
By combining the mktime () and date () functions, you can obtain information about the date input by a specific user. What if you want to find the first Sunday (or any day of the week) from a specific date? First, write a function that will output the appropriate format:
<? Php
Functiondisplay_form (){
Global $ PHP_SELF;
$ Dotw = array ('sunday', 'monday', 'tuesday', 'wednesday', 'thursday ',
'Friday', 'saturday ');
$ Months = array (1 => 'january ', 'february', 'march', 'marril', 'may', 'june ',
'July', 'August ', 'September', 'October ', 'november', 'december ');
?>
<Form target = "<? Php echo $ PHP_SELF;?> "METHOD = GET>
Find the first
<Select name = "dotw">
<? Php
For ($ I = 0; $ I <7; $ I ++ ){
Echo "<OPTION> $ dotw [$ I]";
}
Echo '</SELECT> after <select name = "month"> ';
For ($ I = 1; $ I <= 12; $ I ++ ){
Echo "<option value = \" $ I \ "> $ months [$ I]";
}
Echo '</SELECT> <select name = "day"> ';
For ($ I = 1; $ I <= 31; $ I ++ ){
Echo "<OPTION> $ I ";
}
Echo '</SELECT>, <select name = "year"> ';
$ Start_year = date ('y')-10;
$ End_year = $ start_year + 20;
For ($ I = $ start_year; $ I <= $ end_year; $ I ++ ){
Echo "<OPTION> $ I ";
}
Echo '<input type = "HIDDEN" NAME = "stage" VALUE = "process"> ';
Echo '</SELECT> <input type = "SUBMIT" VALUE = "Do it! "7> </FORM> ';
}
?>
This includes some functions that we haven't discussed so far. The $ months array is written slightly differently, because the January index needs to be set to 1 rather than 0. In addition, it is easier for the program to automatically generate this form than to manually list the values of all the form elements. Therefore, the start part of the form at the top is display_form () the only function that does not belong to the PHP mode. In addition, using the data ("Y") to set the variables $ start_year and the variable $ end_year can easily process the time range from 10 years before and after this year.
The following are functions for processing forms:
<? Php
Function process_form (){
Global $ dotw;
Global $ month;
Global $ day;
Global $ year;
$ Timestamp = mktime (0, 0, $ month, $ day, $ year );
$ Next_dotw = '';
$ Next_timestamp = $ timestamp;
While ($ next_dotw! = $ Dotw ){
$ Next_timestamp ++ = 86400;
$ Next_dotw = date ('l', $ next_timestamp );
}
$ Formatted_first = date ('f d, y', $ timestamp );
$ Formatted_next = date ('f d, y', $ next_timestamp );
Echo "The first $ dotw after $ formatted_first is $ formatted_next .";
}
?>
First, this code converts the date to the Unix timestamp format. If we need to be more careful, we can add some code to ensure that the date we get is within the valid date range, but we do not need to do so at the moment.
When the number of weeks in the next day is not the number of weeks entered by the user, the cycle continues. When the values are different, the total number of seconds represented by the date is increased (also 86400 seconds = 24 hours * 60 minutes * 60 seconds ), then, the number of weeks represented by that number of seconds is recalculated.
Once the number of weeks is the same, the process_form () function will output a correct message:
The first Sunday after Jun 25,199 9 is June 27,199 9.
We also need the familiar main loop to bind these functions together:
<? Php
If (empty ($ stage) {display_form ();}
Else {process_form ();}
?>
The date Processing code sometimes needs to be more complex to process Timestamp and different time zones. However, the date () and mktime () functions are basic tools for processing Unix timestamp transformations, and they are easy to perform in algorithms, and express the date and time in an intuitive way. The date () and mktime () functions operate on the time zone of the machine. If you want to use Greenwich Mean Time (GMT), you can use the gmdate () and gmmktime () functions ().
For example, for a computer in the eastern United States (four hours behind Greenwich Mean Time ):
<? Php
$ Today = mktime (, 0 );
Echo 'Here it is '. date ('G: I: s a, F d, y', $ today );
Echo '';
Echo 'in GMT it is '. gmdate ('G: I: s a, F d, y', $ today );
?>
Will display
Here it is 12:00:00, June 25,199 9
In GMT it is 4:00:00, June 25,199 9
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.