Correct no of days/weeks between 2 days? - php

I have created a function to work out the amount of days and then weeks between 2 dates, in the example below there is 35 days resulting exactly 5 weeks, however the function is returning just short of this at 4.8571428571429 - the division is killing the remainder and coming out at 4.
I could use the ceil function to round it up to 5 but is this this a safe method for all dates? or is there a better way to do it?
$date1 = new DateTime('2015-02-23');
$date2 = new DateTime('2015-03-29');
$diff = $date2->diff($date1)->format("%a");
$weeks = $diff / 7;
return $weeks;

$date2->add(new DateInterval('P1D'));
Use this line after your $date2 initialisation.
Why will this work?
If you count, you have 34 days now: FROM 23-02 0:00 TILL 29-03 0:00.
If you want to count this last day also (to have 35 days), you'll need to add an extra day.

Add a single day to $date2
$date1 = new DateTime('2015-02-23');
$date2 = new DateTime('2015-03-29');
$date2->add(new DateInterval('P1D'));
$diff = $date2->diff($date1)->format("%a");
$weeks = $diff / 7;
return $weeks;
However, you still don't have a whole number of weeks difference with your original dates

Related

Days spent between now and n date [PHP]

Okay! so we all have seen how to get days remaining between NOW and a DATE in the future Like I have in my simple code below:
SAY: $endate = a date in future (5 days from today);
$start = new DateTime();
$end = new DateTime($enddate);
$diff = $end->diff($start)->format('%a');
$days = intval($diff);
echo $days.'Days Remaining'; // 5 days Remaining
The above PHP Code is expected to show you how many days are left between NOW and the FUTURE DATE.
But what I want is the reverse of this situation. That is.
instead of having
5 Days Remaining
I need some thing like
0 Day(s) Spent //where today is day 0 of 5
$start = new DateTime("2021-12-14");
$now = new DateTime();
$now_diff = $now->diff($start)->format("%a");
print_r($now_diff .' Day(s) Spent');
Finding the number of days between two dates
CLUE TO ANSWER PROVIDED BY #KHIMAJI VALUKIYA in comment
The trick here is to set the start date not as current date but the actual date to start counting/calculating from.
Then the end date should be set to the current date.
$start = new DateTime($enddate); //date to start counting from
$end = new DateTime(); //current date
$diff = $end->diff($start)->format('%a');
$days = intval($diff);
echo $days.'Days Spent'; // 5 Days Spent

Carbon show time different days hours minutes second

i try to make time difference with carbon
$dt = Carbon::parse('2018-07-15 00:00:00');
$now = Carbon::now('Asia/Dubai'); //set current time
$seconds = $now->diffInSeconds( $dt ); //difference turn into second
$days = $dt->diffInDays($dt->copy()->addSeconds($seconds));
$hours = $dt->diffInHours($dt->copy()->addSeconds($seconds)->subDays($days));
$minutes = $dt->diffInMinutes($dt->copy()->addSeconds($seconds)->subHours($hours));
$days result are 12 (its right).
$hours result are 8 (seems not right).
$minutes result are 17299 (clearly wrong).
how to get the result for example 12 day 5 hours 45 minutes
Actually functions like diffInSeconds give total difference in seconds that's why the number is so large,to get the minutes for the time difference right you can use -:
$minutes = ($now->minute - $dt->minute);

Laravel division by zero if date is today

I'm trying to make some stats for my website.
But I have the problem that if I use the code I got now, when it is a dy like today, so the first day of the month, I got the error division by zero.
My code:
$begin = date('Y-m-01');
$end = date('Y-m-t');
$begin = new \DateTime($begin);
$end = new \DateTime('now');
$diff = $end->diff($begin); // creates a DateInterval object
$days = (int)$diff->format('%a'); // %a --> days
$average_visits = $get_visits / $days;
It does work, when the day isn't 01 or 1.
How can I fix his issue?
It is because $days actually is zero!
Explanation
Today is 2015-07-01 (at least here at my timezone)
you define $begin = date('Y-m-01'); which is 2015-07-01
you define $end = new \DateTime('now'); which is 2015-07-01
you want to know the difference in days which is 0
$diff = $end->diff($begin);
$days = (int)$diff->format('%a');
you divide a unknown number $get_visits by $days (which is zero).
So you get the correct error division by zero.
Possible Solution
If you mean that $days fully includes the beginning day and the end day then use
$days = (int)$diff->format('%a') + 1;
Then
a) begin = 2015-07-01, end = 2015-07-01
b) begin = 2015-07-01, end = 2015-07-05
a) means this is 1 day
b) means these are 5 days (day 2015-07-01, day 2015-07-02, day 2015-07-03, day 2015-07-04 and day 2015-07-05 makes 5 days)

date_diff returns 0 if the difference is over a year

I am using this function to get month difference between two dates.
$interval = date_diff(date_create('2015-10-08'), date_create('2014-10-10'));
$total_months = $interval->format('%m');
RESULT: 11 (That's Correct!)
But, When the difference is over a year, then,
$interval = date_diff(date_create('2015-11-08'), date_create('2014-10-10'));
$total_months = $interval->format('%m');
RESULT: 0 (That's Wrong!)
why is it returns 0? Is there any way by which I can get difference between any 2 dates? Thanks!
2015-11-08 to 2014-10-10 is 12 months become a year. So it returns 0 month. Calculate the number of years from the $interval then add (year * 12) to the number of months. Example here...
$interval = date_diff(date_create('2015-11-08'), date_create('2014-10-10'));
$year = $interval->format('%Y');
echo $total_months = $interval->format('%m') + $year * 12;
Or better:
$total_months = $interval->y * 12 + $interval->m;
You have years and months as fields, no need to use format to obtain parts as texts for later adding the parts!
It is a bit tricky and needs a work round , try the following, that could
be it.
$first = new DateTime('2015-11-08',new DateTimeZone('America/New_York'));
$second = new DateTime('2014-10-10',new DateTimeZone('America/New_York'));
$diff = $second->diff($first);
$months = (Int)($diff->days/30);
echo "The two dates have $months months between them.";
Output:The two dates have 13 months between them.

Count total month

I have to find total months between two dates in Unix timestamp formats. I want to create a PHP function for that. I've tried this:
get_total_month($startunixdate, $endunixdate) {
$monthdiff = $endunixdate-$startunixdate;
$monthdiff = $monthdiff / 60*60*24*31;
return $monthdiff;
}
Does this function consider leap years as well as month with 30 and 31 separately, or it will just count an approximate month?
Your answer is in this line;
$monthdiff = $monthdiff / 60*60*24*31
This will just count a month based on 31 days. The code divides the seconds by 60 to get the number of minutes, 60 again to get hours, 24 to get number of days, then it uses 31 as the length of a month.
This will result in an approximation of the number of months. For instance, if you take timestamps at the beginning and end of February (start of March), you will usually have a timestamp difference equivalent to 28 days (29 in a leap year). Dividing that by 31 will give you a fraction less than 1, since you are using 31 to represent a full month, when in reality a whole calendar month has passed.
In order to do this, use the DateTime class.
function get_total_month($start, $end) {
// Create DateTime objects
$dt1 = new DateTime($start);
$dt2 = new DateTime($end);
// Get DateInterval object representing difference between the two
$diff = $dt1->diff($dt2); // OR: $diff = date_diff($dt1, $dt2);
// Print the "months" out
echo $diff->format("Difference: %R%m months"); // OR access $diff->m manually
U can use PHP 5.3 datetime method.
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
Reference: PHP DateTime

Categories