PHP date and time _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Date and time in PHP. 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 in PHP there are many easy-to-use functions to display and process the date.
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.
$ 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:
$ D = time ();For ($ I = 0; $ I <10; $ I ++ ){Echo''. Date ('F d', $ d );$ D + = 86400;}?>
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. Every The value is displayed, and the value is calculated based on the Unix Timestamp. the displayed text is set to month, 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:
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 ');
?>
';
}
?>
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:
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:
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 ):
$ 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

Bytes. 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 in...

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.