Date and time in PHP

Source: Internet
Author: User
Tags date format current time functions time zones range requires
PHP has a number of easy to use functions to display and process dates.



To display a date or time in a specific format, use the date () function. It has two parameters: how to display the format of the date and the time stamp that represents the date you want to display. This timestamp must be the total number of seconds previously mentioned, starting from 1970 (if you want to use the time () function in the current period, this function will return the "Now" timestamp). Date () Has many formatting options, like the strftime () function in C or the Posix::strftime () function of the Perl language.

<?php

$birthday _stamp = mktime (19,45,0,3,10,1975);

$birthday _formatted = Date (' F D, Y-g:ia ', $birthday _stamp);

Echo "David is born on $birthday _formatted."

?>

Will show

David is 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 is known. Because you already know what your format will be. These functions are useful when processing a form output part that requires the user to select a date:

<select name= "When" >

<?php

$d = time ();

for ($i = 0; $i < $i + +) {

Echo ' <option value= '. $d. ' > '. Date (' F d ', $d);

$d + 86400;

}

?>

</SELECT>

The above output is a single marquee with 10 options-today and nine days later. Before the program loop starts, we store the current time in the variable $d. Each <OPTION> value is displayed, and the value is calculated as a Unix timestamp, and 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 86,400 (the total number of seconds 24 hours a day-24 hours * 60 Minutes * 60 seconds).

By combining the Mktime () and date () functions, you can come up with information about the date that a particular user entered. What if you were looking for 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 ', ' April ', ' may ', ' June ',

' July ', ' August ', ' September ', ' October ', ' November ', ' December ';

?>

<form target= "<?php echo $PHP _self;?>" method=get>

Find the The

<select name= "DOTW" >

<?php

for ($i = 0; $i < 7; $i + +) {

echo "<OPTION> $DOTW [$i]";

}

Echo ' </SELECT> after <select name= ' month ' > ';

for ($i = 1; $i <= $i + +) {

echo "<option value=\" $i \ "> $months [$i]";

}

Echo ' </SELECT> <select name= ' Day ' > ';

for ($i = 1; $i <= $i + +) {

echo "<OPTION> $i";

}

Echo ' </select&gt, <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> ';

}

?>

There are some functions that we haven't discussed so far. The $months array is slightly different because it requires a January index of 1, not 0. In addition, it is easier for a program to automatically generate this form than to manually list all of the form element's value one by one, so the first part of the form at the top is the only one in the Display_form () function that is not part of the PHP schema. In addition, the use of data ("Y") to set variable $start_year and variable $end_year can be easily processed from 10 years from this year to the time range.
Here are the functions that work with the form:



<?php

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" $dotw $formatted _first is $formatted _next. "

}

?>

First, this code converts the resulting date into the Unix timestamp form. If we need to be more cautious, we can add some code to ensure that the date is within the legal date range, but we do not need to do so for the time being.

The loop continues when we are looking for a "next day" number of weeks that is not the number of one weeks that the user entered. When different, the total number of seconds represented by the date is increased (same is 86,400 seconds = 24 hours * 60 Minutes * 60 seconds), and the number of weeks represented by that second number is recalculated.

Once the number of weeks is the same, the Process_form () function outputs a line of correct messages:

The Sunday after June and 1999 is June 27, 1999.

We also need the familiar Master loop to bundle these functions together:

<?php

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 the Mktime () functions are basic tools for processing UNIX timestamp transformations, which are simple to operate on algorithms and visually express dates and times. The function date () and mktime () operate on the time zone in which the machine is located. If you want to use GMT (GMT), you can use the function gmdate () and Gmmktime ().

For example, for a computer that is in the Eastern Daylight Time (four hours behind GMT):

<?php

$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 is '. Gmdate (' G:i:s A, F D, Y ', $today);

?>

will display

Here it is 12:00:00 pm, June 25, 1999

In GMT it is 4:00:00 pm, June 25, 1999



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.