php weeks between 2 dates
echo datediff('ww', '9 July 2003', '4 March 2004', false);
Find the function on the site below:
http://www.addedbytes.com/code/php-datediff-function/
UPDATE
Link is now broken (Sept 2017), so function below pulled from webarchive:
<?php
/**
* @param $interval
* @param $datefrom
* @param $dateto
* @param bool $using_timestamps
* @return false|float|int|string
*/
function datediff($interval, $datefrom, $dateto, $using_timestamps = false)
{
/*
$interval can be:
yyyy - Number of full years
q - Number of full quarters
m - Number of full months
y - Difference between day numbers
(eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
d - Number of full days
w - Number of full weekdays
ww - Number of full weeks
h - Number of full hours
n - Number of full minutes
s - Number of full seconds (default)
*/
if (!$using_timestamps) {
$datefrom = strtotime($datefrom, 0);
$dateto = strtotime($dateto, 0);
}
$difference = $dateto - $datefrom; // Difference in seconds
$months_difference = 0;
switch ($interval) {
case 'yyyy': // Number of full years
$years_difference = floor($difference / 31536000);
if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
$years_difference--;
}
if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
$years_difference++;
}
$datediff = $years_difference;
break;
case "q": // Number of full quarters
$quarters_difference = floor($difference / 8035200);
while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
$months_difference++;
}
$quarters_difference--;
$datediff = $quarters_difference;
break;
case "m": // Number of full months
$months_difference = floor($difference / 2678400);
while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
$months_difference++;
}
$months_difference--;
$datediff = $months_difference;
break;
case 'y': // Difference between day numbers
$datediff = date("z", $dateto) - date("z", $datefrom);
break;
case "d": // Number of full days
$datediff = floor($difference / 86400);
break;
case "w": // Number of full weekdays
$days_difference = floor($difference / 86400);
$weeks_difference = floor($days_difference / 7); // Complete weeks
$first_day = date("w", $datefrom);
$days_remainder = floor($days_difference % 7);
$odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
if ($odd_days > 7) { // Sunday
$days_remainder--;
}
if ($odd_days > 6) { // Saturday
$days_remainder--;
}
$datediff = ($weeks_difference * 5) + $days_remainder;
break;
case "ww": // Number of full weeks
$datediff = floor($difference / 604800);
break;
case "h": // Number of full hours
$datediff = floor($difference / 3600);
break;
case "n": // Number of full minutes
$datediff = floor($difference / 60);
break;
default: // Number of full seconds (default)
$datediff = $difference;
break;
}
return $datediff;
}
PHP - calculating the number of weeks between two dates
The dates you pass to strtotime
need to be enclosed in quotes. And the correct answer is indeed 3, as there are 3 weeks, 6 days, 23 hours and 59 minutes between the two times. Try this:
$HowManyWeeks = date( 'W', strtotime( '2019-04-21 23:59:00' ) ) - date( 'W', strtotime( '2019-03-25 00:00:00' ) );
echo $HowManyWeeks;
As has been pointed out, this will only work when the weeks are in the same year. It's easier to use DateTime
objects as in @MiroslavGlamuzina answer or you can simply divide the strtotime
difference by 604800 (seconds in a week); you can then take the floor
or ceil
if required to convert to an integer value:
$HowManyWeeks = (strtotime( '2019-04-21 23:59:00' ) - strtotime( '2019-03-25 00:00:00' )) / 604800;
echo $HowManyWeeks;
Output:
3.9939484126984
Demo on 3v4l.org
Weeks between two dates (PHP)
Use diff
& calculate the number of weeks depending on the number of days -
$interval = $startDate->diff($endDate);
echo (int)(($interval->days) / 7);
Output
22
PHP Calculate Number of Weeks Between Dates and Round up to Nearest Week
You want ceil
instead of round
.
ceil(abs(strtotime("2016-05-20") - strtotime("2016-05-12")) / 60 / 60 / 24 / 7);
Calculate the weeks between 2 dates manually in PHP
Your approach seems overly complicated:
function weekCounter($startDate,$endDate=null){
//use today as endDate if no date was supplied
$endDate = $endDate? : date('Y-m-d');
//calculate # of full weeks between dates
$secsPerWeek = 60 * 60 * 24 * 7;
$fullWeeks =
floor((strtotime($endDate) - strtotime($startDate))/$secsPerWeek);
$fullMonths = floor($fullWeeks/4);
$weeksRemainder = $fullWeeks % 4; // weeks that don't fit in a month
//increment from 0-base to 1-base, so first week is Week 1. Same with months
$fullMonths++; $weeksRemainder++;
//return months and weeks in an array
return [$fullMonths,$weeksRemainder];
}
You can call the function this way, and capture months and weeks:
//list() will assign the array members from weekCounter to the vars in list
list($months,$weeks) = weekCounter('2016-06-07'); //no end date, so today is used
//now $months and $weeks can be used as you wish
echo "Month: $months, Week: $weeks"; //outputs Month: 2, Week: 2
Live demo
How to get weeks between two dates
Maybe you can Count the days between your two dates and divide by 7?
PHP check if date between two dates
Edit: use
<=
or>=
to count today's date.
This is the right answer for your code. Just use the strtotime() php function.
$paymentDate = date('Y-m-d');
$paymentDate=date('Y-m-d', strtotime($paymentDate));
//echo $paymentDate; // echos today!
$contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
$contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));
if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)){
echo "is between";
}else{
echo "NO GO!";
}
Get week numbers with start and end dates between two dates in php
Another alternative if to use DateTime
class with the ->modify
method. It helps to you adjusts the date via relative dates.
Here's the idea: (the markup is up to you, or put it inside an array, whichever you prefer)
$i = 1; // week number starts at 1
$date1 = new DateTime('2018-03-21');
$date2 = new DateTime('2018-05-14');
while (true) {
echo "Week {$i}\n";
echo $date1->format('Y-m-d'), ' - ';
$date1->modify('next saturday');
if ($date1 >= $date2) {
echo $date2->format('Y-m-d');
break; // stop the loop, reached the end date, echo the last date, or whatever you want
}
echo $date1->format('Y-m-d') , "\n";
$date1->modify('next monday'); // start again on monday
$i++; // increment for next week
}
Here's a sample fiddle
Sidenote: If you want to know more or for in-depth info about what you can use on relative dates, here's the documentation entry:
http://php.net/manual/en/datetime.formats.relative.php
Related Topics
Get Start and End Days for a Given Week in PHP
Laravel - How to Get Current User in Appserviceprovider
How to Store JavaScript Variable Output into a PHP Variable
PHP Array VS [ ] in Method and Variable Declaration
Basic Simplexml Working Example
How to Force an Image Download in the Browser
Ajaxify Header Cart Items Count in Woocommerce
How to Get Array Key from Corresponding Array Value
Sort Xml via Attribute Value PHP
How to Access Discriminator Field from PHP in Doctrine2
How to Get Values Inside <![Cdata[Values]] > Using PHP Dom
Php: Check Who Had Read Sent Email
Fatal Error: Call to Undefined Function Mb_Strlen()
What Security Problems Could Come from Exposing PHPinfo() to End Users