How many weeks are inside two dates

I have date start and end dates in my database (MySQL). How can I get an answer, how many weeks (or days) are inside these two dates? (mysql or php)

For example, I have a database like this:

Launched and | will_end
2009-12-17 | 2009-12-24
2009-12-12 | 2009-12-26
...

Update to the question:
How to use DATEDIFF? How can I make this work? Or should I use DATEDIFF completely differently?

SELECT DATEDIFF ('Started', 'will_end') AS 'Duration' FROM my_table WHERE id = '110';

+3
source share
6 answers

MySQL datediff, , MySQL 4.1.1.

, DATEDIFF(expr1,expr2) expr1 – expr2, . expr1 expr2 - . .

+1

$d1 $d2 unix, time(), :

$diffweek = abs($d1 - $d2) / 604800; 

, DATETIME, :

$diffweek = abs(strtotime($d1) - strtotime($d2)) / 604800; 

p/s: 604800 - (60 * 60 * 24 * 7)

p/s2: intval($diffweek) round($diffweek)

+2

. 7, , .

, start_date = "2010-12-26" end_date = "2011-01-25" W51,52,01,02,03,04, 6 ISO, 7, 5.

, .

- start_date, 28 .

function weeks($ladate2,$ladate3) {
    $start_week= date("W",strtotime($ladate2));
    $end_week= date("W",strtotime($ladate3));
    $number_of_weeks= $end_week - $start_week;

    $weeks=array();
    $weeks[]=$start_week;
    $increment_date=$ladate2;
    $i="1";

    if ($number_of_weeks<0){
        $start_year=date("Y",strtotime($ladate2));
        $last_week_of_year= date("W",strtotime("$start_year-12-28"));
        $number_of_weeks=($last_week_of_year-$start_week)+$end_week;
    }

    while ($i<=$number_of_weeks)
    {
        $increment_date=date("Y-m-d", strtotime($ladate2. " +$i week"));
        $weeks[]=date("W",strtotime($increment_date));

        $i=$i+1;
    }

    return $weeks;
}

function diff_weeks($ladate2,$ladate3) {
    $weeks=weeks($ladate2,$ladate3);
    $diff_weeks=count($weeks);

    return $diff_weeks;
}

, Manikam

+2
+1
0
<?php
  $dayDif    = date('z',strtotime('2009-12-17)') - date('z',strtotime('2009-12-24)');
  $numWeeks  = $dayDif / 7;
?>

The z parameter for the php date function gives you the day of the year (0 - 365). Subtracting the two values, you will find how many days between dates. Then a factor of seven for the number of weeks.

Read this page carefully, the date () function is rich. http://php.net/manual/en/function.date.php

-2
source

Source: https://habr.com/ru/post/1726336/


All Articles