Calculate days in months between two dates with PHP
I have a period with startdate of 2016-12-26 and end date 2017-03-04.
Now I would like to find out how many days in each months there is, from a given period. Expected output from the above period dates (array):
2016-12: 5
2017-01: 31
2017-02: 28
2017-03: 4
How can I accomplish this cleanest way? I have tried to:
- first looking at the period_start, get the days = 26 and
- find out the start/end dates of the months between 2016-12 and 2017-03, to then calculate the days here (31 respectively 28 in february)
- then finally calculating the 4 days in 2017-03.
But is there any cleaner/better way?
php
add a comment |
I have a period with startdate of 2016-12-26 and end date 2017-03-04.
Now I would like to find out how many days in each months there is, from a given period. Expected output from the above period dates (array):
2016-12: 5
2017-01: 31
2017-02: 28
2017-03: 4
How can I accomplish this cleanest way? I have tried to:
- first looking at the period_start, get the days = 26 and
- find out the start/end dates of the months between 2016-12 and 2017-03, to then calculate the days here (31 respectively 28 in february)
- then finally calculating the 4 days in 2017-03.
But is there any cleaner/better way?
php
Check this - stackoverflow.com/questions/2040560/…
– Suresh
Apr 24 '17 at 9:24
2
if you start from2016-12-26then2016-12would be5not26
– Niklesh Raut
Apr 24 '17 at 9:30
yes exactly i i also wants to know how your desired days are calculated ?
– Bunker Boy
Apr 24 '17 at 9:37
1
just count the seconds between the dates then convert it to days
– Gert
Apr 24 '17 at 9:37
@Niklesh This is very true. My bad, 2016-12 should be 5
– Karem
Apr 24 '17 at 10:41
add a comment |
I have a period with startdate of 2016-12-26 and end date 2017-03-04.
Now I would like to find out how many days in each months there is, from a given period. Expected output from the above period dates (array):
2016-12: 5
2017-01: 31
2017-02: 28
2017-03: 4
How can I accomplish this cleanest way? I have tried to:
- first looking at the period_start, get the days = 26 and
- find out the start/end dates of the months between 2016-12 and 2017-03, to then calculate the days here (31 respectively 28 in february)
- then finally calculating the 4 days in 2017-03.
But is there any cleaner/better way?
php
I have a period with startdate of 2016-12-26 and end date 2017-03-04.
Now I would like to find out how many days in each months there is, from a given period. Expected output from the above period dates (array):
2016-12: 5
2017-01: 31
2017-02: 28
2017-03: 4
How can I accomplish this cleanest way? I have tried to:
- first looking at the period_start, get the days = 26 and
- find out the start/end dates of the months between 2016-12 and 2017-03, to then calculate the days here (31 respectively 28 in february)
- then finally calculating the 4 days in 2017-03.
But is there any cleaner/better way?
php
php
edited Apr 24 '17 at 10:41
Karem
asked Apr 24 '17 at 9:21
KaremKarem
5,98463152258
5,98463152258
Check this - stackoverflow.com/questions/2040560/…
– Suresh
Apr 24 '17 at 9:24
2
if you start from2016-12-26then2016-12would be5not26
– Niklesh Raut
Apr 24 '17 at 9:30
yes exactly i i also wants to know how your desired days are calculated ?
– Bunker Boy
Apr 24 '17 at 9:37
1
just count the seconds between the dates then convert it to days
– Gert
Apr 24 '17 at 9:37
@Niklesh This is very true. My bad, 2016-12 should be 5
– Karem
Apr 24 '17 at 10:41
add a comment |
Check this - stackoverflow.com/questions/2040560/…
– Suresh
Apr 24 '17 at 9:24
2
if you start from2016-12-26then2016-12would be5not26
– Niklesh Raut
Apr 24 '17 at 9:30
yes exactly i i also wants to know how your desired days are calculated ?
– Bunker Boy
Apr 24 '17 at 9:37
1
just count the seconds between the dates then convert it to days
– Gert
Apr 24 '17 at 9:37
@Niklesh This is very true. My bad, 2016-12 should be 5
– Karem
Apr 24 '17 at 10:41
Check this - stackoverflow.com/questions/2040560/…
– Suresh
Apr 24 '17 at 9:24
Check this - stackoverflow.com/questions/2040560/…
– Suresh
Apr 24 '17 at 9:24
2
2
if you start from
2016-12-26 then 2016-12 would be 5 not 26– Niklesh Raut
Apr 24 '17 at 9:30
if you start from
2016-12-26 then 2016-12 would be 5 not 26– Niklesh Raut
Apr 24 '17 at 9:30
yes exactly i i also wants to know how your desired days are calculated ?
– Bunker Boy
Apr 24 '17 at 9:37
yes exactly i i also wants to know how your desired days are calculated ?
– Bunker Boy
Apr 24 '17 at 9:37
1
1
just count the seconds between the dates then convert it to days
– Gert
Apr 24 '17 at 9:37
just count the seconds between the dates then convert it to days
– Gert
Apr 24 '17 at 9:37
@Niklesh This is very true. My bad, 2016-12 should be 5
– Karem
Apr 24 '17 at 10:41
@Niklesh This is very true. My bad, 2016-12 should be 5
– Karem
Apr 24 '17 at 10:41
add a comment |
5 Answers
5
active
oldest
votes
This can be achieved easily using the DateTime class. Create the objects, and use DateTime::diff() on them, then use the days property.
$start = new DateTime("2016-12-26");
$end = new DateTime("2017-03-04");
echo $start->diff($end)->days; // Output: 68
Live demo
- http://php.net/datetime.construct
Thanks, but I look for a solution that breaks down the days into months rather than the sum for the whole period.
– Karem
Apr 24 '17 at 10:42
add a comment |
@Karem hope this logic will help you, this is working case for all your conditions please try this below one:
<?php
$startDate = '2016-12-26';
$endDate = '2017-03-04';
$varDate = $startDate;
while($varDate < $endDate)
$d = date('d', strtotime($varDate));
$Y = date('Y', strtotime($varDate));
$m = date('m', strtotime($varDate));
$days = cal_days_in_month(CAL_GREGORIAN,$m,$Y);
$time = strtotime($varDate);
if($varDate == $startDate)
$time = strtotime(date('Y-m-01', $time));
$days = $days - $d;
else if(date("Y-m", strtotime($varDate)) == date("Y-m", strtotime($endDate)))
$days = date("j", strtotime($endDate));
echo date('Y-m', strtotime($varDate)). ": ".$days."<br>";
$varDate = date('Y-m-d', strtotime("+1 month", $time));
Thanks your for this! The per month is correct, but amount of days is wrong (26 should be 5, sorry for the mistake in question). So it should calculate how many days in there is in the month from 26th december (5). Also the static if($startDate == '2016-12-26' || $startDate == '2017-03-01') seems like a "quickfix" more than a proper solution that would work for any date periods
– Karem
Apr 24 '17 at 10:52
@Karem i have asked in the comment box, i thought it should be 26 ok let me update my answer
– Bunker Boy
Apr 24 '17 at 10:54
1
@Karem please check my updated answer, this is according to you
– Bunker Boy
Apr 24 '17 at 11:02
Always try to add live demo : eval.in/781778
– Niklesh Raut
Apr 24 '17 at 12:47
@ Niklesh i ll remember (y)
– Bunker Boy
Apr 24 '17 at 12:48
add a comment |
This is long but easy to understand that how to achieve you your goal
<?php
function getMonthDays($start,$end)
if($start < $end)
$start_time = strtotime($start);
$last_day_of_start = strtotime(date("Y-m-t",$start_time));
$start_month_days = ($last_day_of_start - $start_time)/(60*60*24);
echo date("Y-m",$start_time).": ".$start_month_days."n";
$days = "";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
while($start < $end)
$month = date("m",$start_time);
$year = date("Y",$start_time);
$days = date('t', mktime(0, 0, 0, $month, 1, $year));
echo date("Y-m",$start_time).": ".$days."n";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
echo date("Y-m",strtotime($end)).": ".date("d",strtotime($end))."n";
else
echo "Wrong Input";
getMonthDays('2016-12-26','2017-03-04');
?>
live demo : https://eval.in/781724
Function returns array : https://eval.in/781741
I like this solution. Thank you for contribution. But why +1 month? Case: if period_start is the same as period_end, it should be 0 days. Case 2: getMonthDays('2016-12-26','2016-12-27'); fails
– Karem
Apr 25 '17 at 9:09
Added both conditions also : eval.in/782363.
– Niklesh Raut
Apr 25 '17 at 9:51
Great, thanks for this! Makes sense. Any way the day can be counted aswell? getMonthDays('2016-12-26','2016-12-27'); should return 2, not 1.
– Karem
Apr 25 '17 at 11:44
add a comment |
<?php
$d1 = strtotime('2016-12-26');
$d2 = strtotime('2017-03-04');
echo floor(($d2 - $d1)/(60*60*24));
?>
Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, how does this code produce a line for each month?
– Toby Speight
Apr 24 '17 at 11:48
add a comment |
i used Carbon (https://carbon.nesbot.com/docs/) but you can do it with any other time lib.
$startDate = Carbon::createFromFormat('!Y-m-d', '2017-01-11');;
$endDate = Carbon::createFromFormat('!Y-m-d', '2018-11-13');;
$diffInMonths = $endDate->diffInMonths($startDate);
for ($i = 0; $i <= $diffInMonths; $i++)
$start = $i == 0 ? $startDate->copy()->addMonth($i) : $startDate->copy()->addMonth($i)->firstOfMonth();
$end = $diffInMonths == $i ? $endDate->copy() : $start->copy()->endOfMonth();
echo $end->format('Y-m') . ' ' . ($end->diffInDays($start) + 1) . PHP_EOL;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43584254%2fcalculate-days-in-months-between-two-dates-with-php%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
This can be achieved easily using the DateTime class. Create the objects, and use DateTime::diff() on them, then use the days property.
$start = new DateTime("2016-12-26");
$end = new DateTime("2017-03-04");
echo $start->diff($end)->days; // Output: 68
Live demo
- http://php.net/datetime.construct
Thanks, but I look for a solution that breaks down the days into months rather than the sum for the whole period.
– Karem
Apr 24 '17 at 10:42
add a comment |
This can be achieved easily using the DateTime class. Create the objects, and use DateTime::diff() on them, then use the days property.
$start = new DateTime("2016-12-26");
$end = new DateTime("2017-03-04");
echo $start->diff($end)->days; // Output: 68
Live demo
- http://php.net/datetime.construct
Thanks, but I look for a solution that breaks down the days into months rather than the sum for the whole period.
– Karem
Apr 24 '17 at 10:42
add a comment |
This can be achieved easily using the DateTime class. Create the objects, and use DateTime::diff() on them, then use the days property.
$start = new DateTime("2016-12-26");
$end = new DateTime("2017-03-04");
echo $start->diff($end)->days; // Output: 68
Live demo
- http://php.net/datetime.construct
This can be achieved easily using the DateTime class. Create the objects, and use DateTime::diff() on them, then use the days property.
$start = new DateTime("2016-12-26");
$end = new DateTime("2017-03-04");
echo $start->diff($end)->days; // Output: 68
Live demo
- http://php.net/datetime.construct
answered Apr 24 '17 at 9:42
QirelQirel
10.7k62540
10.7k62540
Thanks, but I look for a solution that breaks down the days into months rather than the sum for the whole period.
– Karem
Apr 24 '17 at 10:42
add a comment |
Thanks, but I look for a solution that breaks down the days into months rather than the sum for the whole period.
– Karem
Apr 24 '17 at 10:42
Thanks, but I look for a solution that breaks down the days into months rather than the sum for the whole period.
– Karem
Apr 24 '17 at 10:42
Thanks, but I look for a solution that breaks down the days into months rather than the sum for the whole period.
– Karem
Apr 24 '17 at 10:42
add a comment |
@Karem hope this logic will help you, this is working case for all your conditions please try this below one:
<?php
$startDate = '2016-12-26';
$endDate = '2017-03-04';
$varDate = $startDate;
while($varDate < $endDate)
$d = date('d', strtotime($varDate));
$Y = date('Y', strtotime($varDate));
$m = date('m', strtotime($varDate));
$days = cal_days_in_month(CAL_GREGORIAN,$m,$Y);
$time = strtotime($varDate);
if($varDate == $startDate)
$time = strtotime(date('Y-m-01', $time));
$days = $days - $d;
else if(date("Y-m", strtotime($varDate)) == date("Y-m", strtotime($endDate)))
$days = date("j", strtotime($endDate));
echo date('Y-m', strtotime($varDate)). ": ".$days."<br>";
$varDate = date('Y-m-d', strtotime("+1 month", $time));
Thanks your for this! The per month is correct, but amount of days is wrong (26 should be 5, sorry for the mistake in question). So it should calculate how many days in there is in the month from 26th december (5). Also the static if($startDate == '2016-12-26' || $startDate == '2017-03-01') seems like a "quickfix" more than a proper solution that would work for any date periods
– Karem
Apr 24 '17 at 10:52
@Karem i have asked in the comment box, i thought it should be 26 ok let me update my answer
– Bunker Boy
Apr 24 '17 at 10:54
1
@Karem please check my updated answer, this is according to you
– Bunker Boy
Apr 24 '17 at 11:02
Always try to add live demo : eval.in/781778
– Niklesh Raut
Apr 24 '17 at 12:47
@ Niklesh i ll remember (y)
– Bunker Boy
Apr 24 '17 at 12:48
add a comment |
@Karem hope this logic will help you, this is working case for all your conditions please try this below one:
<?php
$startDate = '2016-12-26';
$endDate = '2017-03-04';
$varDate = $startDate;
while($varDate < $endDate)
$d = date('d', strtotime($varDate));
$Y = date('Y', strtotime($varDate));
$m = date('m', strtotime($varDate));
$days = cal_days_in_month(CAL_GREGORIAN,$m,$Y);
$time = strtotime($varDate);
if($varDate == $startDate)
$time = strtotime(date('Y-m-01', $time));
$days = $days - $d;
else if(date("Y-m", strtotime($varDate)) == date("Y-m", strtotime($endDate)))
$days = date("j", strtotime($endDate));
echo date('Y-m', strtotime($varDate)). ": ".$days."<br>";
$varDate = date('Y-m-d', strtotime("+1 month", $time));
Thanks your for this! The per month is correct, but amount of days is wrong (26 should be 5, sorry for the mistake in question). So it should calculate how many days in there is in the month from 26th december (5). Also the static if($startDate == '2016-12-26' || $startDate == '2017-03-01') seems like a "quickfix" more than a proper solution that would work for any date periods
– Karem
Apr 24 '17 at 10:52
@Karem i have asked in the comment box, i thought it should be 26 ok let me update my answer
– Bunker Boy
Apr 24 '17 at 10:54
1
@Karem please check my updated answer, this is according to you
– Bunker Boy
Apr 24 '17 at 11:02
Always try to add live demo : eval.in/781778
– Niklesh Raut
Apr 24 '17 at 12:47
@ Niklesh i ll remember (y)
– Bunker Boy
Apr 24 '17 at 12:48
add a comment |
@Karem hope this logic will help you, this is working case for all your conditions please try this below one:
<?php
$startDate = '2016-12-26';
$endDate = '2017-03-04';
$varDate = $startDate;
while($varDate < $endDate)
$d = date('d', strtotime($varDate));
$Y = date('Y', strtotime($varDate));
$m = date('m', strtotime($varDate));
$days = cal_days_in_month(CAL_GREGORIAN,$m,$Y);
$time = strtotime($varDate);
if($varDate == $startDate)
$time = strtotime(date('Y-m-01', $time));
$days = $days - $d;
else if(date("Y-m", strtotime($varDate)) == date("Y-m", strtotime($endDate)))
$days = date("j", strtotime($endDate));
echo date('Y-m', strtotime($varDate)). ": ".$days."<br>";
$varDate = date('Y-m-d', strtotime("+1 month", $time));
@Karem hope this logic will help you, this is working case for all your conditions please try this below one:
<?php
$startDate = '2016-12-26';
$endDate = '2017-03-04';
$varDate = $startDate;
while($varDate < $endDate)
$d = date('d', strtotime($varDate));
$Y = date('Y', strtotime($varDate));
$m = date('m', strtotime($varDate));
$days = cal_days_in_month(CAL_GREGORIAN,$m,$Y);
$time = strtotime($varDate);
if($varDate == $startDate)
$time = strtotime(date('Y-m-01', $time));
$days = $days - $d;
else if(date("Y-m", strtotime($varDate)) == date("Y-m", strtotime($endDate)))
$days = date("j", strtotime($endDate));
echo date('Y-m', strtotime($varDate)). ": ".$days."<br>";
$varDate = date('Y-m-d', strtotime("+1 month", $time));
edited Apr 24 '17 at 11:06
answered Apr 24 '17 at 10:23
Bunker BoyBunker Boy
1,5051928
1,5051928
Thanks your for this! The per month is correct, but amount of days is wrong (26 should be 5, sorry for the mistake in question). So it should calculate how many days in there is in the month from 26th december (5). Also the static if($startDate == '2016-12-26' || $startDate == '2017-03-01') seems like a "quickfix" more than a proper solution that would work for any date periods
– Karem
Apr 24 '17 at 10:52
@Karem i have asked in the comment box, i thought it should be 26 ok let me update my answer
– Bunker Boy
Apr 24 '17 at 10:54
1
@Karem please check my updated answer, this is according to you
– Bunker Boy
Apr 24 '17 at 11:02
Always try to add live demo : eval.in/781778
– Niklesh Raut
Apr 24 '17 at 12:47
@ Niklesh i ll remember (y)
– Bunker Boy
Apr 24 '17 at 12:48
add a comment |
Thanks your for this! The per month is correct, but amount of days is wrong (26 should be 5, sorry for the mistake in question). So it should calculate how many days in there is in the month from 26th december (5). Also the static if($startDate == '2016-12-26' || $startDate == '2017-03-01') seems like a "quickfix" more than a proper solution that would work for any date periods
– Karem
Apr 24 '17 at 10:52
@Karem i have asked in the comment box, i thought it should be 26 ok let me update my answer
– Bunker Boy
Apr 24 '17 at 10:54
1
@Karem please check my updated answer, this is according to you
– Bunker Boy
Apr 24 '17 at 11:02
Always try to add live demo : eval.in/781778
– Niklesh Raut
Apr 24 '17 at 12:47
@ Niklesh i ll remember (y)
– Bunker Boy
Apr 24 '17 at 12:48
Thanks your for this! The per month is correct, but amount of days is wrong (26 should be 5, sorry for the mistake in question). So it should calculate how many days in there is in the month from 26th december (5). Also the static if($startDate == '2016-12-26' || $startDate == '2017-03-01') seems like a "quickfix" more than a proper solution that would work for any date periods
– Karem
Apr 24 '17 at 10:52
Thanks your for this! The per month is correct, but amount of days is wrong (26 should be 5, sorry for the mistake in question). So it should calculate how many days in there is in the month from 26th december (5). Also the static if($startDate == '2016-12-26' || $startDate == '2017-03-01') seems like a "quickfix" more than a proper solution that would work for any date periods
– Karem
Apr 24 '17 at 10:52
@Karem i have asked in the comment box, i thought it should be 26 ok let me update my answer
– Bunker Boy
Apr 24 '17 at 10:54
@Karem i have asked in the comment box, i thought it should be 26 ok let me update my answer
– Bunker Boy
Apr 24 '17 at 10:54
1
1
@Karem please check my updated answer, this is according to you
– Bunker Boy
Apr 24 '17 at 11:02
@Karem please check my updated answer, this is according to you
– Bunker Boy
Apr 24 '17 at 11:02
Always try to add live demo : eval.in/781778
– Niklesh Raut
Apr 24 '17 at 12:47
Always try to add live demo : eval.in/781778
– Niklesh Raut
Apr 24 '17 at 12:47
@ Niklesh i ll remember (y)
– Bunker Boy
Apr 24 '17 at 12:48
@ Niklesh i ll remember (y)
– Bunker Boy
Apr 24 '17 at 12:48
add a comment |
This is long but easy to understand that how to achieve you your goal
<?php
function getMonthDays($start,$end)
if($start < $end)
$start_time = strtotime($start);
$last_day_of_start = strtotime(date("Y-m-t",$start_time));
$start_month_days = ($last_day_of_start - $start_time)/(60*60*24);
echo date("Y-m",$start_time).": ".$start_month_days."n";
$days = "";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
while($start < $end)
$month = date("m",$start_time);
$year = date("Y",$start_time);
$days = date('t', mktime(0, 0, 0, $month, 1, $year));
echo date("Y-m",$start_time).": ".$days."n";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
echo date("Y-m",strtotime($end)).": ".date("d",strtotime($end))."n";
else
echo "Wrong Input";
getMonthDays('2016-12-26','2017-03-04');
?>
live demo : https://eval.in/781724
Function returns array : https://eval.in/781741
I like this solution. Thank you for contribution. But why +1 month? Case: if period_start is the same as period_end, it should be 0 days. Case 2: getMonthDays('2016-12-26','2016-12-27'); fails
– Karem
Apr 25 '17 at 9:09
Added both conditions also : eval.in/782363.
– Niklesh Raut
Apr 25 '17 at 9:51
Great, thanks for this! Makes sense. Any way the day can be counted aswell? getMonthDays('2016-12-26','2016-12-27'); should return 2, not 1.
– Karem
Apr 25 '17 at 11:44
add a comment |
This is long but easy to understand that how to achieve you your goal
<?php
function getMonthDays($start,$end)
if($start < $end)
$start_time = strtotime($start);
$last_day_of_start = strtotime(date("Y-m-t",$start_time));
$start_month_days = ($last_day_of_start - $start_time)/(60*60*24);
echo date("Y-m",$start_time).": ".$start_month_days."n";
$days = "";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
while($start < $end)
$month = date("m",$start_time);
$year = date("Y",$start_time);
$days = date('t', mktime(0, 0, 0, $month, 1, $year));
echo date("Y-m",$start_time).": ".$days."n";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
echo date("Y-m",strtotime($end)).": ".date("d",strtotime($end))."n";
else
echo "Wrong Input";
getMonthDays('2016-12-26','2017-03-04');
?>
live demo : https://eval.in/781724
Function returns array : https://eval.in/781741
I like this solution. Thank you for contribution. But why +1 month? Case: if period_start is the same as period_end, it should be 0 days. Case 2: getMonthDays('2016-12-26','2016-12-27'); fails
– Karem
Apr 25 '17 at 9:09
Added both conditions also : eval.in/782363.
– Niklesh Raut
Apr 25 '17 at 9:51
Great, thanks for this! Makes sense. Any way the day can be counted aswell? getMonthDays('2016-12-26','2016-12-27'); should return 2, not 1.
– Karem
Apr 25 '17 at 11:44
add a comment |
This is long but easy to understand that how to achieve you your goal
<?php
function getMonthDays($start,$end)
if($start < $end)
$start_time = strtotime($start);
$last_day_of_start = strtotime(date("Y-m-t",$start_time));
$start_month_days = ($last_day_of_start - $start_time)/(60*60*24);
echo date("Y-m",$start_time).": ".$start_month_days."n";
$days = "";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
while($start < $end)
$month = date("m",$start_time);
$year = date("Y",$start_time);
$days = date('t', mktime(0, 0, 0, $month, 1, $year));
echo date("Y-m",$start_time).": ".$days."n";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
echo date("Y-m",strtotime($end)).": ".date("d",strtotime($end))."n";
else
echo "Wrong Input";
getMonthDays('2016-12-26','2017-03-04');
?>
live demo : https://eval.in/781724
Function returns array : https://eval.in/781741
This is long but easy to understand that how to achieve you your goal
<?php
function getMonthDays($start,$end)
if($start < $end)
$start_time = strtotime($start);
$last_day_of_start = strtotime(date("Y-m-t",$start_time));
$start_month_days = ($last_day_of_start - $start_time)/(60*60*24);
echo date("Y-m",$start_time).": ".$start_month_days."n";
$days = "";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
while($start < $end)
$month = date("m",$start_time);
$year = date("Y",$start_time);
$days = date('t', mktime(0, 0, 0, $month, 1, $year));
echo date("Y-m",$start_time).": ".$days."n";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
echo date("Y-m",strtotime($end)).": ".date("d",strtotime($end))."n";
else
echo "Wrong Input";
getMonthDays('2016-12-26','2017-03-04');
?>
live demo : https://eval.in/781724
Function returns array : https://eval.in/781741
edited Apr 24 '17 at 11:55
answered Apr 24 '17 at 11:45
Niklesh RautNiklesh Raut
19.5k43069
19.5k43069
I like this solution. Thank you for contribution. But why +1 month? Case: if period_start is the same as period_end, it should be 0 days. Case 2: getMonthDays('2016-12-26','2016-12-27'); fails
– Karem
Apr 25 '17 at 9:09
Added both conditions also : eval.in/782363.
– Niklesh Raut
Apr 25 '17 at 9:51
Great, thanks for this! Makes sense. Any way the day can be counted aswell? getMonthDays('2016-12-26','2016-12-27'); should return 2, not 1.
– Karem
Apr 25 '17 at 11:44
add a comment |
I like this solution. Thank you for contribution. But why +1 month? Case: if period_start is the same as period_end, it should be 0 days. Case 2: getMonthDays('2016-12-26','2016-12-27'); fails
– Karem
Apr 25 '17 at 9:09
Added both conditions also : eval.in/782363.
– Niklesh Raut
Apr 25 '17 at 9:51
Great, thanks for this! Makes sense. Any way the day can be counted aswell? getMonthDays('2016-12-26','2016-12-27'); should return 2, not 1.
– Karem
Apr 25 '17 at 11:44
I like this solution. Thank you for contribution. But why +1 month? Case: if period_start is the same as period_end, it should be 0 days. Case 2: getMonthDays('2016-12-26','2016-12-27'); fails
– Karem
Apr 25 '17 at 9:09
I like this solution. Thank you for contribution. But why +1 month? Case: if period_start is the same as period_end, it should be 0 days. Case 2: getMonthDays('2016-12-26','2016-12-27'); fails
– Karem
Apr 25 '17 at 9:09
Added both conditions also : eval.in/782363.
– Niklesh Raut
Apr 25 '17 at 9:51
Added both conditions also : eval.in/782363.
– Niklesh Raut
Apr 25 '17 at 9:51
Great, thanks for this! Makes sense. Any way the day can be counted aswell? getMonthDays('2016-12-26','2016-12-27'); should return 2, not 1.
– Karem
Apr 25 '17 at 11:44
Great, thanks for this! Makes sense. Any way the day can be counted aswell? getMonthDays('2016-12-26','2016-12-27'); should return 2, not 1.
– Karem
Apr 25 '17 at 11:44
add a comment |
<?php
$d1 = strtotime('2016-12-26');
$d2 = strtotime('2017-03-04');
echo floor(($d2 - $d1)/(60*60*24));
?>
Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, how does this code produce a line for each month?
– Toby Speight
Apr 24 '17 at 11:48
add a comment |
<?php
$d1 = strtotime('2016-12-26');
$d2 = strtotime('2017-03-04');
echo floor(($d2 - $d1)/(60*60*24));
?>
Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, how does this code produce a line for each month?
– Toby Speight
Apr 24 '17 at 11:48
add a comment |
<?php
$d1 = strtotime('2016-12-26');
$d2 = strtotime('2017-03-04');
echo floor(($d2 - $d1)/(60*60*24));
?>
<?php
$d1 = strtotime('2016-12-26');
$d2 = strtotime('2017-03-04');
echo floor(($d2 - $d1)/(60*60*24));
?>
answered Apr 24 '17 at 9:34
Gopi ChandGopi Chand
1146
1146
Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, how does this code produce a line for each month?
– Toby Speight
Apr 24 '17 at 11:48
add a comment |
Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, how does this code produce a line for each month?
– Toby Speight
Apr 24 '17 at 11:48
Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, how does this code produce a line for each month?
– Toby Speight
Apr 24 '17 at 11:48
Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. In particular, how does this code produce a line for each month?
– Toby Speight
Apr 24 '17 at 11:48
add a comment |
i used Carbon (https://carbon.nesbot.com/docs/) but you can do it with any other time lib.
$startDate = Carbon::createFromFormat('!Y-m-d', '2017-01-11');;
$endDate = Carbon::createFromFormat('!Y-m-d', '2018-11-13');;
$diffInMonths = $endDate->diffInMonths($startDate);
for ($i = 0; $i <= $diffInMonths; $i++)
$start = $i == 0 ? $startDate->copy()->addMonth($i) : $startDate->copy()->addMonth($i)->firstOfMonth();
$end = $diffInMonths == $i ? $endDate->copy() : $start->copy()->endOfMonth();
echo $end->format('Y-m') . ' ' . ($end->diffInDays($start) + 1) . PHP_EOL;
add a comment |
i used Carbon (https://carbon.nesbot.com/docs/) but you can do it with any other time lib.
$startDate = Carbon::createFromFormat('!Y-m-d', '2017-01-11');;
$endDate = Carbon::createFromFormat('!Y-m-d', '2018-11-13');;
$diffInMonths = $endDate->diffInMonths($startDate);
for ($i = 0; $i <= $diffInMonths; $i++)
$start = $i == 0 ? $startDate->copy()->addMonth($i) : $startDate->copy()->addMonth($i)->firstOfMonth();
$end = $diffInMonths == $i ? $endDate->copy() : $start->copy()->endOfMonth();
echo $end->format('Y-m') . ' ' . ($end->diffInDays($start) + 1) . PHP_EOL;
add a comment |
i used Carbon (https://carbon.nesbot.com/docs/) but you can do it with any other time lib.
$startDate = Carbon::createFromFormat('!Y-m-d', '2017-01-11');;
$endDate = Carbon::createFromFormat('!Y-m-d', '2018-11-13');;
$diffInMonths = $endDate->diffInMonths($startDate);
for ($i = 0; $i <= $diffInMonths; $i++)
$start = $i == 0 ? $startDate->copy()->addMonth($i) : $startDate->copy()->addMonth($i)->firstOfMonth();
$end = $diffInMonths == $i ? $endDate->copy() : $start->copy()->endOfMonth();
echo $end->format('Y-m') . ' ' . ($end->diffInDays($start) + 1) . PHP_EOL;
i used Carbon (https://carbon.nesbot.com/docs/) but you can do it with any other time lib.
$startDate = Carbon::createFromFormat('!Y-m-d', '2017-01-11');;
$endDate = Carbon::createFromFormat('!Y-m-d', '2018-11-13');;
$diffInMonths = $endDate->diffInMonths($startDate);
for ($i = 0; $i <= $diffInMonths; $i++)
$start = $i == 0 ? $startDate->copy()->addMonth($i) : $startDate->copy()->addMonth($i)->firstOfMonth();
$end = $diffInMonths == $i ? $endDate->copy() : $start->copy()->endOfMonth();
echo $end->format('Y-m') . ' ' . ($end->diffInDays($start) + 1) . PHP_EOL;
answered Nov 15 '18 at 12:20
Michał ChwedkowskiMichał Chwedkowski
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43584254%2fcalculate-days-in-months-between-two-dates-with-php%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Check this - stackoverflow.com/questions/2040560/…
– Suresh
Apr 24 '17 at 9:24
2
if you start from
2016-12-26then2016-12would be5not26– Niklesh Raut
Apr 24 '17 at 9:30
yes exactly i i also wants to know how your desired days are calculated ?
– Bunker Boy
Apr 24 '17 at 9:37
1
just count the seconds between the dates then convert it to days
– Gert
Apr 24 '17 at 9:37
@Niklesh This is very true. My bad, 2016-12 should be 5
– Karem
Apr 24 '17 at 10:41