Discussion: How to use PHP to calculate the year, month, week, and day of two date intervals _php tutorial

Source: Internet
Author: User
Use the PHP implementation to calculate the year, month, week, and day of two date intervals:
Copy CodeThe code is as follows:
function format ($a, $b) {
Check the two date size, the default is small after large, if the former large after the small swap position to ensure that the former small after large
if (Strtotime ($a) >strtotime ($b)) list ($a, $b) =array ($b, $a);
$start = Strtotime ($a);
$stop = Strtotime ($b);
$extend = ($stop-$start)/86400;
$result [' extends '] = $extend;
if ($extend <7) {//if less than 7 days direct return days
$result [' daily '] = $extend;
}elseif ($extend <=31) {///less than 28 days returns the number of weeks, as leap year February satisfies
if ($stop ==strtotime ($a. ' +1 month ')) {
$result [' monthly '] = 1;
}else{
$w = Floor ($extend/7);
$d = ($stop-strtotime ($a. ' + '. $w. ' Week ')/86400;
$result [' weekly '] = $w;
$result [' daily '] = $d;
}
}else{
$y = Floor ($extend/365);
if ($y >=1) {//If more than one year
$start = Strtotime ($a. ' + '. $y. ' year ');
$a = date (' y-m-d ', $start);
Judging whether it really has been a year, if not, then cut
if ($start > $stop) {
$a = date (' y-m-d ', strtotime ($a. '-1 month '));
$m = 11;
$y--;
}
$extend = ($stop-strtotime ($a))/86400;
}
if (Isset ($m)) {
$w = Floor ($extend/7);
$d = $extend-$w *7;
}else{
$m = Isset ($m)? $m: Round ($extend/30);
$stop >=strtotime ($a. ' + '. $m. ' Month ')? $m: $m-;
if ($stop >=strtotime ($a. ' + '. $m. ' Month ')} {
$d = $w = ($stop-strtotime ($a. ' + '. $m. ' Month ')/86400;
$w = Floor ($w/7);
$d = $d-$w *7;
}
}
$result [' yearly '] = $y;
$result [' monthly '] = $m;
$result [' weekly '] = $w;
$result [' daily '] = Isset ($d)? $d: null;
}
Return Array_filter ($result);
}

Print_r (Format (' 2012-10-1 ', ' 2012-12-15 '));
?>

Operation Result:
Array ([extends]=>75[monthly]=>2[weekly]=>2)

PHP queries the number of weeks in a day and the start date of the corresponding week
Copy the Code code as follows:
/**
* @file
* @version 1.1
* @author QQ83989686
* @date last modified on 2012-8-7
* @brief
*/
Gets the week number of a date, the start end time for the week
Private Function Getweekstartendday ($day)
{
$g = strftime ("%u", Strtotime ($day));
Return Array (' Week_num ' =>strftime ("%V", Strtotime ($day)), ' Week_start_day ' =>strftime ('%y-%m-%d ', Strtotime ($ Day)-($g-1) *86400), ' WEEK_START_DAY_CN ' =>strftime ('%y year%m month%d ', Strtotime ($day)-($g-1) *86400), ' week_end_day ' = >strftime ('%y-%m-%d ', Strtotime ($day) + (7-$g) *86400), ' WEEK_END_DAY_CN ' =>strftime ('%Y year%m month%d day ', Strtotime ($ Day) + (7-$g) *86400));
}

http://www.bkjia.com/PHPjc/327561.html www.bkjia.com true http://www.bkjia.com/PHPjc/327561.html techarticle Use the PHP implementation to calculate the year, month, week, and day of two date intervals: The copy code code is as follows:? PHP function Format ($a, $b) {//Check two date size, default before small after large, if previous ...

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