Php-mysql Course

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 ( https://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 ( https://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()

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Days between two dates, or of a specified week, in PHP MySQL

Last accessed pages

  1. querySelector and querySelectorAll (30136)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142540)
  3. JavaScript strip_tags and stripslashes (8825)
  4. Date and Time in ActionScript 3 (10086)
  5. Bind Tool and Control Points (4430)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (543)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)