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

Source: Internet
Author: User

Use the PHP implementation to calculate the number of years, months, weeks, and days for two date intervals:

Copy Code code as follows:

<?php
function format ($a, $b) {
Check two date size, the default is small after the big, if the former large and small then swap position to ensure that before small after the big
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 the number of days returned directly is less than 7 days
$result [' daily '] = $extend;
}elseif ($extend <=31) {///less than 28 days returns the number of weeks, due to the leap year February met
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);
It's been a year since you've been judged, but if you don't, you can cut it.
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 '));
?>

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

PHP Query the number of weeks in the day and the start date of the corresponding week

Copy Code code as follows:

/**
* @file
* @version 1.1
* @author QQ83989686
* @date 2012-8-7 Last modification time
* @brief
*/
Gets the week number of a date, the start end time of 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%m month%d days ', 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 days ', Strtotime ($ Day) + (7-$g) *86400);
}

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.