This question already has answers here:
Calculate the number of months between two dates in PHP?
(16 answers)
Closed 6 years ago.
I would like to calculate number of calendar months between two days. For example:
2017-11-01 => 2017-11-30 = 1 (the same month)
2017-11-01 => 2017-12-01 = 2 (two different calendar months in the date)
2017-11-30 => 2017-12-01 = 2 (the same as above)
2017-11-15 => 2018-06-01 = 8 (6 + 2 different calendar months in the date)
How can I do that? So far I have:
$monthStart = new DateTime('2017-11-01');
$monthEnd = new DateTime('2017-12-01');
$dateDiff = $monthStart->diff($monthEnd);
$maxMonths = ($dateDiff->m + ($dateDiff->y*12) + $dateDiff->d>0?1:0);
But it doesn't work for some dates.
This QUESTION IS DIFFERENT from: Calculate the number of months between two dates in PHP? as I want to calculate the month every time the number for month changes. Not only when there is a 30 days difference between two days. Check the examples I have provided.
$a = "2007-01-01";
$b = "2008-05-31";
$i = date("Ym", strtotime($a));
while($i <= date("Ym", strtotime($b))){
echo $i."<br>";
if(substr($i, 4, 2) == "12")
$i = (date("Y", strtotime($i."01")) + 1)."01";
else
$i++;
}
output:
200701
200702
200703
200704
200705
200706
200707
200708
200709
200710
200711
200712
200801
200802
200803
200804
200805
Related
This question already has answers here:
PHP add up two time variables
(7 answers)
Closed 7 years ago.
How to sum up time in php.
For example I have this series of time duration logs:
00:10:00
00:30:10
01:00:50
The total should be 1 hour and 41 minutes
Here is my code:
$log_in = new DateTime($log->log_in);
$log_out = new DateTime($log->log_out);
$diff = $log_out->diff($log_in);
$total += strtotime($diff->format('%H:%i:%s'));
echo $diff->format('%H:%i:%s');
Convert the time into timestamp using strtotime() function. Then manipulate the time according your need and get result in terms of seconds.
Once you get seconds.
For Hour
$hour = $diff % 3600
For Minute
$Minute = ($diff - ( $hour *3600))%60;
This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 7 years ago.
I have check-in column and check-out column in my mysql table. I am posting this dates with UI-Datepicker.
eg:
I am trying to block the dates in the datepicker. I have dates like this in database
check in = 2015-06-15
check out = 2015-06-20
Now i want to get dates like this
2015-06-15,
2015-06-16,
2015-06-17,
2015-06-18,
2015-06-19,
2015-06-20
How to get dates like above I mentioned. So please help me and resolve my problem.
<?php
$date_from = strtotime("10 September 2000");
$date_to = strtotime("15 September 2000");
function list_days($date_from,$date_to){
$arr_days = array();
$day_passed = ($date_to - $date_from); //seconds
$day_passed = ($day_passed/86400); //days
$counter = 1;
$day_to_display = $date_from;
while($counter < $day_passed){
$day_to_display += 86400;
//echo date("F j, Y \n", $day_to_display);
$arr_days[] = date('o-m-d',$day_to_display);
$counter++;
}
return $arr_days;
}
print_r(list_days($date_from,$date_to));
?>
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
PHP calculate days Between 2 different dates [duplicate]
(2 answers)
Closed 9 years ago.
$1date =$row['Date1'];
$2date = $row['Date2'];
$datediff = $1date - $2date;
echo $datediff;
I want to count the days between and put into a table the result(10 dollars for each day passed)
// convert to unix timestamp
$1date = strtotime($row['Date1']);
$2date = strtotime($row['Date2']);
// 86400 seconds in a day
// floor to round down, change to ceil to round up
$datediff = floor(($1date - $2date) / 86400);
$cost = $days * 10;
You could do it in MySQL then work it into a variable. Do the initial call
SELECT TIMESTAMPDIFF('Date1','Date2');
Try:
$days = date_diff(date_create($row['Date1']), date_create($row['Date2']))->format('%a');
$cost = $days * 10;
I'm going to write a function to print a number of days left between two dates. I would like it to tell the months and days left. For example:
45 days = 1month, 15 days
65 days = 2months, 5 days
10 days = 10 days
So I tried:
<?
$days=50;
if($days>"31"){
$time=$days/30;
}
echo $time;//1.67 month
?>
According to the code above. I expect the result to be like:
1 month, 20 days
Could you guys please suggest.
Try:
$days = 50;
if ($days > 31){
$month = floor($days/30); // return lowest whole integer
$days = $days % 30; // calculate left days
}
echo $month . " => " . $days; // output `1 => 20`
Get the month number for both months and subtract. Add in calculation for year change
Get day of months for both dates. If date2 > date1, subtract and you have the number of days, else remove 1 from month count and sumteact date
I have a number of X days, let's say 700.
How can I output this 700 to years?
Eg. 700 = 1.11 years (1 year 11 months)
or 2.2 (2 years and two months)
... And so on
I don't care for leap years or such, just need an aproximate value.
Think about it...
700 days, 30 days in a month.
700/30 = 23.33 months. 12 months in a year.
23.33/12 = 1.944444... take the integer value (int)1.944444 to get 1 for the year.
23.33%12= 11.33... months. You can cast it to an int as well to get 11
Your result: 1 year, 11 months
try this one...
//86400 seconds per day
//31556926 seconds per year
$days = 700;
$timestamp = (86400 * $days) / 31556926;
echo $timestamp;
// = 1.9165364839402 years
use this site for referrence: http://www.epochconverter.com/ they have conversion per month, week, days.
//1.9165364839402