Count total month - php

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

Related

How to check if the difference between 2 dates is bigger than 15 minutes?

I am using this function to compare a date from the database to the current date, and i need to check if the difference between the 2 dates is bigger than 15 minutes but i don't know how to do that, i think i need to do something like if($comp > 0 days 0 hours 15 minutes)
function TimeOut($dateP){
$date = new DateTime(date('Y-m-d H:i:s'));
$date2 = new DateTime($dateP);
echo $comp = $date->diff($date2)->format("%d days %h hours and %i minuts %s seconds");
if ($comp > "15 minutes ?") {
return true;
}
}
You can use diff and then read the m parameter of the result. In the example below $difference will be DateInterval object:
$difference = $start_date->diff($date2);
if($difference->i > 15) {
echo "difference greater than 15 minutes"
}
A date interval stores either a fixed amount of time (in years,
months, days, hours etc) or a relative time string in the format that
DateTime's constructor supports.

Difference between 2 DateTimes in Hours in Decimal Format

I have 2 DateTimes, I need to calculate the difference between them in hours in decimal format. The hard part is making sure the result is storing the value to 2 decimal places.
$datetime1 = new DateTime("2017-09-01 23:00:00");
$datetime2 = new DateTime();
$difference = $datetime2->diff($datetime1);
But the result is just a whole number which loses too much accuracy for me. How to keep the value to 2 decimal places?
DateTime::diff returns DaterInterval that has a whole number for each individual time related property.
The days property does not account for increments less than a day, as they are accumulated to and removed from the lesser properties and then is rounded down.
So $diff->h will never be greater than 23. While $diff->s and $diff->i will never be greater than 59. Where days will contain the total days within the year and month properties. Not to be confused with $diff->d, which is the incremental number of days.
In order to determine the total hours using diff, you just need to perform math on each of the properties to retrieve the number of hours of the property.
Example: https://3v4l.org/KhBQC
$datetime1 = new DateTime('2017-09-01 23:00:00');
$datetime2 = new DateTime('2017-09-02 01:34:00');
$diff = $datetime2->diff($datetime1);
$hours = round($diff->s / 3600 + $diff->i / 60 + $diff->h + $diff->days * 24, 2);
echo $hours; //2.57
In php 7.1 you can also account for microseconds by adding $diff->f / 3.6e+9.
A more simplistic approach would be to subtract the unix timestamps, to retrieve the total number of seconds between the two dates. Then divide the remaining seconds by the number of seconds in an hour (3600).
Example: https://3v4l.org/SbjEU
$datetime1 = new DateTime('2017-09-01 23:00:00');
$datetime2 = new DateTime('2017-09-02 01:34:00');
$hours = round(($datetime2->getTimestamp() - $datetime1->getTimestamp()) / 3600, 2);
echo $hours; //2.57
2 decimal places:
$datetime1 = new DateTime("2017-09-01 23:00:00");
$datetime2 = new DateTime();
$epoch1 = $datetime1->getTimestamp();
$epoch2 = $datetime2->getTimestamp();
$diff = $epoch1 - $epoch2;
echo number_format( $diff / 3600, '2' );

Collect the number of days between two dates [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find number of days between two dates using php
Collect the number of days between two dates, for example
02/11/2012
And between
02/12/2012
The result is the number of days = 1 day
try this
function dateDiff ($d1, $d2) {
return round(abs(strtotime($d1)-strtotime($d2))/86400);
}
The function uses the PHP ABS() absolute value to always return a postive number as the number of days between the two dates.
PHP >= 5.3 you can use DateTime::Diff:
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
echo (strtotime('02/12/2012') - strtotime('02/11/2012')) / (24*60*60);
This line converts both dates to unix timestamps, subtracts them and divides this by the amount of seconds in a day.
You can convert your date to a timestamp with the strtotime() function:
$date1 = strtotime("02/11/2012");
$date2 = strtotime("02/12/2012");
$difference = $date1 - $date2;
Then you have the difference in seconds which is a new timestamp, so you can convert this to days with the date() function:
$days = date("d", $difference);

PHP Compare a difference of timestamp and a real time

I would like to compare a difference between two timestamps and a time in base 60 or 10.
More precisely if timestamp1-timestamp2 is longer (or no) than x hours and y seconds.
I'm using DateTime and DateInterval classes, but there isn't such a function, and i don't find a clean solution.
Thanks
$time1 = new DateTime("2011-01-26 01:13:30"); // string date
$time2 = new DateTime();
$time2->setTimestamp(1327560334); // timestamps, it can be string date too.
$interval = $time2->diff($time1);
echo $interval->format("%H hours %i minutes %s seconds");
Output
11 hours 32 minutes 4 seconds
$timestamp1 = strtotime('2012-01-26 14:00:00');
$timestamp2 = strtotime('2012-01-25 17:00:00');
if (abs($timestamp1 - $timestamp2) < 60 * 60 * 5 /* (5 hours) */) {
...
Convert both the timestamp and the real time to the UNIX timestamp. Then simply subtract to get the number of seconds difference.
With datetime objects:
$interval = $TempReceiptDateTime->diff($ShipDateTime);
echo $interval->format('%R%H:%I:%s days');
more

Difference between dates [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Difference between dates
How to calculate the date difference between 2 dates using php
So, I have two dates. For instance, 2010-09-24 and 2010-09-25. I want to check if between those two dates the difference is 1 day.
I don't need to get a value of 86400 which means one day in seconds.
Don't forget that it can be 28, 28, 29, 30, 31 days in month.
Thanks.
What I have right now, but it doesn't work when there is difference between months:
$strto = strtotime($date);
$d1 = date('d', $strto);
$d2 = date('d', time());
echo $d2- $d1;
You can use strtotime to get the number of seconds in between
echo abs(strtotime('2010-09-24') - strtotime('2010-09-25'));
Don't use the day value - (eg date('d', ...)) - leave it as an integer (the result of strtotime()). Then subtract those dates, and then get the floor(difference / 86400).
Like so:
$dt = strtotime($date);
echo floor(abs(time() - $dt) / 86400);
You can do this nicely with the DateTime class if you have PHP 5.3:
<?php
$datetime1 = new DateTime('2010-09-25');
$datetime2 = new DateTime('2010-09-26');
$interval = $datetime1->diff($datetime2);
$intervaldays = (int) $interval->format('%R%d'); // %R signs the result +/-
This is probably less efficient than using strtotime methods, but it is very readable.
Why are you using date('d'... which returns the day of the month?
strtotime will create a UNIX-timestamp which is exactly what time() returns, so abs(time() - strtotime($date)) should already do the job. This way you don't have to worry how many days a month has as you're only working with timestamps.
This will get you the number of (complete) days:
floor( abs(time() - strtotime($date)) / 86400 )

Categories