Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to calculate the total weeks between two dates in PHP

2 Answers

0 votes
function total_week_between_two_dates($dt1, $dt2) {
    $datetimeobject1 = DateTime::createFromFormat('m/d/Y', $dt1);
    $datetimeobject2 = DateTime::createFromFormat('m/d/Y', $dt2);
    
    if ($dt1 > $dt2) {
        return total_week_between_two_dates($dt2, $dt1);
    }
    
    return floor($datetimeobject1->diff($datetimeobject2)->days/7);
}

$dt1 = '1/1/2019';
$dt2 = '3/12/2019';

echo total_week_between_two_dates($dt1, $dt2);

 
/*
run:
      
10
     
*/

 



answered Mar 12, 2019 by avibootz
0 votes
function total_week_between_two_dates($dt1, $dt2) {
    $date1 = new DateTime($dt1);
    $date2 = new DateTime($dt2);

    $interval = $date1->diff($date2);

    return floor(($interval->days) / 7);
}

$dt1 = '1/1/2019';
$dt2 = '3/12/2019';

echo total_week_between_two_dates($dt1, $dt2);

 
/*
run:
      
10
     
*/

 



answered Mar 12, 2019 by avibootz

Related questions

1 answer 79 views
1 answer 138 views
1 answer 123 views
3 answers 334 views
1 answer 94 views
1 answer 103 views
...