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 to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Days between two dates, or of a specified week, in PHP MySQL

Last accessed pages

  1. Shape Tween - Flash Animation (6149)
  2. The Mastery of Love (7440)
  3. Get Mime Type of file or string content in PHP (6230)
  4. Countdown Timer with starting time added into a form (11533)
  5. Disable button and Enable it after specified time (17533)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (72)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)