Friday 26 June 2015

Days between two dates, or of a specified week, in PHP MySQL

This tutorial shows how to calculate the days between two dates, also, how to get the days of a specified week of year, in PHP and MySQL.

Calculate number of days between two dates

• To calculate the number of days between two dates in PHP, you can use this code:
<?php
// calculate number of days between $enddate and $startdate
$startdate = '2012-04-3';
$enddate = '2012-05-18';        // for current date:  date('Y-m-d');
$days = (strtotime($enddate) - strtotime($startdate)) / (60 * 60 * 24);

echo $days;     // 45
?>

If you want to calculate the number of days between a specified date and current day, use:   $enddate = date('Y-m-d');

- To select the records between two dates in a MySQL database, you can use this example:
SELECT * FROM `table` WHERE `date` BETWEEN '2012-04-03' AND '2012-05-18'

- To select between a specified date and current date, use this query:
SELECT * FROM `table` WHERE `date` BETWEEN '2012-04-03' AND CURRENT_DATE

Get the days of a week of year

• To get the days of a specified week in PHP, from Monday to Sunday inclusively (or starting with any week day), you can use this function:
// function to get the days of a specified week ( http://coursesweb.net )
// returns an array with days of a specified $week_nr of $year
// $week_nr represents the week number of year
// by default $startday is 1 (0=Sunday, 1=Monday, 2=Tuesday, ...)
function daysWeek($week_nr, $year, $startday=1) {
  $setweek = $week_nr;
  $re = array();      // stores the result to return

  // traverse the number of the days in a week, 7
  for($i=0; $i<7; $i++){
    // set day of week according to $startday
    $setday = $startday + $i;

    // if $setday is 7 or higher, decrease 7 days, and increment $setweek with one week
    if($setday >= 7) {
      $setday -= 7;
      $setweek = $week_nr + 1;
    }

    // adds day number in $re
    $re[] = date('d', strtotime($year.'W'.$setweek.$setday));
  }

  return $re;
}

The third parameter, $startday, sets the starting day of week, by default is 1 (for days from Monday to Sunday inclusively), and it is optional.
If you want to get the days starting with other week day (other then Monday), pass to the daysWeek() function a third argument with the number of the day of the week, 0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, ..., or date('N') for current day.
- Examples:
$days = daysWeek(20, 2011);     // days of the twentieth week in 2011 (from Monday to Monday)
$days = daysWeek(42, 2012, 0);     // days of the 42nd week in 2012 (from Sunday to Sunday)
$days = daysWeek(date('W')+1, date('Y'));     // days of the next week of current year

Example, gets the days of last week of current year (starting with Monday):
<?php
// function to get the days of a specified week ( http://coursesweb.net )
// returns an array with days of a specified $week_nr of $year
// $week_nr represents the week number of year
// by default $startday is 1 (0=Sunday, 1=Monday, 2=Tuesday, ...)
function daysWeek($week_nr, $year, $startday=1) {
  $setweek = $week_nr;
  $re = array();      // stores the result to return

  // traverse the number of the days in a week, 7
  for($i=0; $i<7; $i++){
    // set day of week according to $startday
    $setday = $startday + $i;

    // if $setday is 7 or higher, decrease 7 days, and increment $setweek with one week
    if($setday >= 7) {
      $setday -= 7;
      $setweek = $week_nr + 1;
    }

    // adds day number in $re
    $re[] = date('d', strtotime($year.'W'.$setweek.$setday));
  }

  return $re;
}

$week_nr = date('W') - 1;      // number of last week
$year = date('Y');             // current year

// get the days of last week (from Monday to Sunday inclusively)
$days = daysWeek($week_nr, $year);

// Test
var_export($days);
?>

- To select the records of a specified week of year in a MySQL database, you can use this example:
SELECT * FROM `table` WHERE YEAR(`date`) = 2012 WEEK(`date`) = 40

- To select the records of the last week, use this SQL query:
SELECT * FROM `table` WHERE `date` BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 WEEK) AND CURDATE()

0 comments:

Post a Comment