Php obtains the instance program of the day of the week

Source: Internet
Author: User

PHP date () parameter description

The w parameter is mainly used here. The explanation of this parameter is:

W indicates the day of the week, and the number indicates 0 (Sunday) to 6 (Saturday)

With this, it is very easy. The author directly posts the code here. The details are not explained:

Simply getting the day of the week is actually the use of the date function

Date ("l ");

// Data can be used to obtain the English week, such as Sunday.

Date ("w ");

// You can obtain the number of weeks, for example, 123. Note that 0 is Sunday.

 

You can obtain the Chinese week.

The code is as follows: Copy code

$ Weekarray = array ("day", "one", "two", "three", "four", "five", "six ");
Echo "week". $ weekarray [date ("w")];

Obtain the specified date:

The code is as follows: Copy code

$ Weekarray = array ("day", "one", "two", "three", "four", "five", "six ");
Echo "week". $ weekarray [date ("w", "2011-11-11")];

Example

The code is as follows: Copy code

<? Php
// Obtain the day of the week in php
Function getWeek ($ unixTime = ''){
$ UnixTime = is_numeric ($ unixTime )? $ UnixTime: time ();
$ Weekarray = array ('day', 'yi', '2', '3', '4', '5', '6 ');
Return 'week'. $ weekarray [date ('W', $ unixTime)];
}
Echo getWeek ();

Or

<? Php
Function getWeek (){
$ Week = date ("w ");
Switch ($ week ){
Case 1:
Return "Monday ";
Break;
Case 2:
Return "Tuesday ";
Break;
Case 3:
Return "Wednesday ";
Break;
Case 4:
Return "Thursday ";
Break;
Case 5:
Return "Friday ";
Break;
Case 6:
Return "Saturday ";
Break;
Case 0:
Return "Sunday ";
Break;
 }
}
Echo "today is:". getWeek ();
?>


The date function of phP has a time range, that is, it can only be from 1970 ~ In 2038, the algorithms beyond this range are inaccurate. How can this problem be solved? There is actually a formula:

Zeller formula: w = y + [y/4] + [c/4]-2c + [26 (m + 1)/10] + D-1

The meanings of the symbols in the formula are as follows: w: week; c: Century-1; y: year (two digits); m: Month (m greater than or equal to 3, less than or equal to 14, that is, in the Caile formula, the period 1 and month 1 of a year are regarded as the period 13 and 14 of the previous year. For example, the period 1 of is counted as the period 13 months and 1 day of the previous year.) d: day; [] indicates an integer. (C: Century minus one, y is the last two digits of the year, M is the month, and d is the number of days. The values of C and y are calculated based on the values of 13 and 14 months of the previous year .)

Divide the calculated W by 7, and the remainder is the day of the week. If the remainder is 0, it is Sunday.

Take October 1, 2049 (100 anniversary of the National Day) as an example, using the Zeller formula for calculation, the process is as follows:
Zeller formula: w = y + [y/4] + [c/4]-2c + [26 (m + 1)/10] + D-1
= 49 + [49/4] + [20/4]-2 × 20 + [26 × (10 + 1)/10] + 1-1
= 49 + [12.25] + 5-40 + [28.6]
= 49 + 12 + 5-40 + 28
= 54 (divided by more than 7 5)
That is, October 1, 2049 (100 anniversary of the National Day) is the 5 th day of the week.

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.