Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php weeks between 2 dates

Tags:

date

php

hi how to find no of weeks and each mondays date between 2 dates. for ex 10-07-2009 to today .

Note :consider leap year and other date related constrains also.

like image 374
ArK Avatar asked Jun 12 '10 11:06

ArK


People also ask

How do I calculate the number of weeks between two dates in PHP?

php function week_between_two_dates($date1, $date2) { $first = DateTime::createFromFormat('m/d/Y', $date1); $second = DateTime::createFromFormat('m/d/Y', $date2); if($date1 > $date2) return week_between_two_dates($date2, $date1); return floor($first->diff($second)->days/7); } $dt1 = '1/1/2014'; $dt2 = '12/31/2014'; ...

How to use date_ diff in PHP?

The date_diff() is an inbuilt function in PHP which is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure. Syntax: date_diff($datetime1, $datetime2);

How to add two dates in PHP?

PHP date_add() Function $date=date_create("2013-03-15"); date_add($date,date_interval_create_from_date_string("40 days")); echo date_format($date,"Y-m-d");


1 Answers

Here's an alternative solution using DateTime:-

function datediffInWeeks($date1, $date2) {     if($date1 > $date2) return datediffInWeeks($date2, $date1);     $first = DateTime::createFromFormat('m/d/Y', $date1);     $second = DateTime::createFromFormat('m/d/Y', $date2);     return floor($first->diff($second)->days/7); }  var_dump(datediffInWeeks('1/2/2013', '6/4/2013'));// 21 

See it working

like image 57
vascowhite Avatar answered Oct 07 '22 02:10

vascowhite