PHP Judging today is the first few weeks of the month

Source: Internet
Author: User
Tags float number
PHP has a very powerful system function, the date () function. Clever use of him can be achieved by displaying any time we need.

Today, for example, there is a need to judge today is the first few weeks of this month, here is not to discuss the question and whether it is meaningful, the following concrete see how to use PHP to implement this function.

This function mainly uses the date () function of the W J two parameters, the date () function parameters are many.

The PHP date () parameter description explains the two parameters of W J as follows:

W represents day of the week, number represents 0 (for Sunday) to 6 (for Saturday)

The day of the J month, the numbers indicate that from 1 to 31 the specific algorithm that uses PHP to judge the week of the month today is: the use of the date (that is, the number) and the total days of the week (7 days) of the relationship, the borrowing Ceil () function directly to draw today is the first few weeks of this month.

The Ceil () function is used to calculate the smallest integer greater than the specified number (floating-point, float number).

For example: Assuming that 3rd is Thursday, then the value of Ceil (3/7) will be 1, indicating that the day is the first Thursday of the month. The next Thursday calculation is Ceil (10/7), which has a value of 2, indicating that 10th is the second Thursday. Other, and so on. Based on this algorithm, you can determine the calculation formula for calculating the number of weeks in the current month is set to: ceil (Date/7).

Let's look at a specific example:

/*
function: Calculate the number of weeks of today's month
*/
Header (' content-type:text/html; Charset=utf-8 ');
$wk _day=date (' W '); Get today is the day of the week
$date _now=date (' J '); What's the date today?
$wkday _ar=array (' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' VI '); Normalize the expression of the Sunday
$cal _result=ceil ($date _NOW/7); Calculation is the first few weeks
$str =date ("Y-year N-month J-Day"). "Week". $wkday _ar[$wk _day]. "-This month". $cal _result. $wkday _ar[$wk _day];
Echo $str;

?>

The results of this run are as follows:

May 21, 2013 Tuesday-3rd Tuesday of the Month

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