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 HTML5 tag can be used to embed an external application (SWF, PDF) in web page?
<mark> <embed> <canvas>
<embed src="flash_game.swf" width="450" height="350" />
Which CSS pseudo-element adds a special style to the first line of a text?
:first-letter :before :first-line
#id:first-line {
  font-weight: bold;
  color: blue;
}
Click on the window object property which gets or sets the URL of current page.
window.location window.self window.status
var url = window.location;
alert(url);
Indicate the PHP function used to get the contents of a file or page and store it into a string.
fopen() file_put_contents() file_get_contents()
$homepage = file_get_contents("http://coursesweb.net/");
echo $homepage;
Days between two dates, or of a specified week, in PHP MySQL

Last accessed pages

  1. CSS Outline (2655)
  2. Disable button and Enable it after specified time (17527)
  3. JavaScript Game - Find the Word (887)
  4. Ajax-PHP Chat Script (49473)
  5. Working with getElementsByTagName (13084)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (318)
  2. CSS cursor property - Custom Cursors (56)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (44)
  4. The Mastery of Love (41)
  5. CSS3 2D transforms (40)