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 adds an image in web page?
<div> <img> <span>
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
}
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
});
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
}
Days between two dates, or of a specified week, in PHP MySQL

Last accessed pages

  1. Get Time Elapsed (1884)
  2. Star shapes with CSS (11169)
  3. Calling Function and Class Method with Name from String (5745)
  4. The Prayer of the Frog (2052)
  5. jQuery Drag and Drop Rows between two similar Tables (12802)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (317)
  2. Read Excel file data in PHP - PhpExcelReader (115)
  3. The Four Agreements (96)
  4. PHP Unzipper - Extract Zip, Rar Archives (91)
  5. The Mastery of Love (85)