Date and time in PHP _php tutorial

Source: Internet
Author: User
Tags time zones
PHP has a number of easy-to-use functions to display and process dates.
To display a date or time in a particular format, you can use the date () function. It has two parameters: how to display the format of a date and the timestamp that represents the date you want to display. This timestamp must be the total number of seconds previously mentioned from 1970 (if you want to use the time () function, this function returns the "Now" timestamp). Date () Has many formatting options, like the strftime () function in the C language or the Posix::strftime () function of the Perl language.
$birthday _stamp = mktime (19,45,0,3,10,1975);
$birthday _formatted = Date (' F D, Y-g:ia ', $birthday _stamp);
Echo "David was born on $birthday _formatted."
?>
are displayed
David was born on March, 1975--7:45 p.m.
Of course, this complex format function is not very useful if you need a specific date that you know. Because you know beforehand what your format will be. These functions are useful when working with a form output section that requires the user to select a date:
$d = time (); for ($i = 0; $i < $i + +) {echo ''. Date (' F d ', $d); $d + = 86400; }?>
The above will output a radio box with 10 options--today and nine days after. Before the program loop begins, we store the current time in the variable $d. Each one Values are displayed, and the values are computed in Unix timestamps, and the displayed text is set to month, Day ("July 27", "July 28", and so on). After displaying the value, the variable $d is added 86,400 (the total number of seconds for 24 hours a day-24 hours * 60 Minutes * 60 seconds).
By combining the Mktime () and date () functions, you can draw information about the date that a particular user entered. What if you're looking for the first Sunday from a certain date (or any day of the week)? 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 ', ' April ', ' may ', ' June ',
' July ', ' August ', ' September ', ' October ', ' November ', ' December ');
?>
';
}
?>
There are some functions that we have not discussed so far. The $months array is slightly different because you need to index the January to 1, not 0. In addition, it is easier for the program to automatically generate this form than to manually list all of the value one by one of this form element, so the top form begins with the only part of the Display_form () function that does not belong to PHP mode. In addition, the use of the data ("Y") to set the variable $start_year and variable $end_year can easily handle the time range of 10 years from this year.
Here are the functions for working with the form:
function Process_form () {
Global $DOTW;
Global $month;
Global $day;
Global $year;
$timestamp = Mktime (0,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 translates the resulting date into a Unix timestamp form. If we need to be more cautious, we can add some code to ensure that the date we get is within the legal date range, but we don't have to do this for the time being.
The cycle continues when we are looking for the "next day" of the week number that is not the number of one weeks entered by the user. When different, the total number of seconds represented by the date is incremented (also 86,400 seconds = 24 hours * 60 Minutes * 60 seconds), then the number of weeks that the second number represents will be recalculated.
Once the day of the week is consistent, the process_form () function outputs a line of correct messages:
The first Sunday after June, 1999 is June 27, 1999.
We also need the familiar main loop to bundle these functions together:
if (empty ($stage)) {display_form ();}
else {process_form ();}
?>
Date-processing code sometimes needs to be more complex to handle daylight saving time and different time zones, but the date () and mktime () functions are essential tools for processing UNIX timestamp transformations, which are simple to operate on the algorithm and express the date and time visually. The function date () and mktime () operate on time in the machine's time zone. If you want to use Greenwich Mean (GMT), you can use the function gmdate () and Gmmktime ().
For example, for a computer that is in the eastern United States daylight saving time (four hours behind Greenwich Mean Time):
$today = Mktime (12,0,0,6,25,1999);
Echo ' Here it's '. Date (' G:i:s A, F D, Y ', $today);
Echo ';
Echo ' in GMT it's '. Gmdate (' G:i:s A, F D, Y ', $today);
?>
will display
Here it's 12:00:00 pm, June 25, 1999
In GMT it's 4:00:00 pm, June 25, 1999

http://www.bkjia.com/PHPjc/317075.html www.bkjia.com true http://www.bkjia.com/PHPjc/317075.html techarticle PHP has a number of easy-to-use functions to display and process dates. To display a date or time in a particular format, you can use the date () function. It has two parameters: how to display the format of the date with ...

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