Calculating years/months/days between dates - php

$date1 = "2000-01-01";
$date2 = "2011-03-14";
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365 * 60 * 60 * 24));
$months = ceil(($diff - ($years * 365 * 60 * 60 * 24)) / ((365 * 60 * 60 * 24) / 12));
$months2 = floor(($diff - ($years * 365 * 60 * 60 * 24)) / ((365 * 60 * 60 * 24) / 12));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months2 * 30 * 60 * 60 * 24)/ (60 * 60 * 24));
The answer I get is 11 years , 2 months and 14 days. Shouldn't it be 11 years, 3 months and 14 days?
I have tried quite a few different ways and I always end up with 2 months instead of 3. Does anyone know why?

Try using PHP's built-in date API instead of doing the math yourself.
Using DateTime, DateInterval and the DateTime::diff function:
$date1 = new DateTime("2000-01-01");
$date2 = new DateTime("2011-03-14");
$diff = $date2->diff($date1);
var_dump($diff);'
/* is prints:
object(DateInterval)#3 (8) {
["y"]=>
int(11)
["m"]=>
int(2)
["d"]=>
int(13)
["h"]=>
int(0)
["i"]=>
int(0)
["s"]=>
int(0)
["invert"]=>
int(1)
["days"]=>
int(4090)
}
*/
At least then you don't need to worry if you made an error (the result seems correct).

The answer that you are getting is completely right!

Related

PHP Date Calculator Returning Wrong Days

I'm using the below calculator to determine the years, months, and days between a set date and the current date. I thought it was working fine, but then it came up on the year mark and i noticed it was not working properly. Tomorrow is actually when the one year mark would be, but it is currently returning 11 months, 34 days. Could anyone tell me whats wrong? It should be 11 months, 30 days.
function relationshipTimer($functionDate)
{
$date1 = $functionDate;
$date2 = date("Y-m-d");
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365 * 60 * 60 * 24));
$months = floor(($diff - ($years * 365 * 60 * 60 * 24)) / ((365 * 60 * 60 * 24) / 12));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
if ($years > 0) {echo $years . " Year";}
if ($years > 1) {echo "s ";}
if ($months > 0) {echo " " . $months . " Month";}
if ($months > 1) {echo "s ";}
if ($date1 == $date2) {echo "1 Day ";}
if ($days > 0) {echo $days . " Day";}
if ($days > 1) {echo "s ";}
}
And this is where $functionDate comes from:
relationshipTimer("2018-04-28");
When you calculate $days, you are assuming that all months are 30 days long, which is obviously wrong. Therefore you get a year that is 11 months plus 35 days (36 days for leap years).
Processing dates is complicated. You should always use specialized tools like PHP's DateTime::diff()
For example, with:
$date1 = new DateTime("2018-04-28");
$date2 = new DateTime("2019-04-27");
$diff = $date2->diff($date1);
print $diff->format("%y years %m months %d days\n");
... you get (because April has 30 days):
0 years 11 months 29 days

Why seconds and minutes do not calculate properly in codeigniter?

I'm currently creating a CI project and i faced the folowing issue.
I want to show "posted "X" time ago" text and i found a script online but the seconds and minutes are not properly shown.
I've already searched all over the net but i couldn't find anything.
Here's my function:
$today = time();
$createdday = mysql_to_unix($ptime);
$datediff = abs($today - $createdday);
$difftext = "";
$years = floor($datediff / (365 * 60 * 60 * 24));
$months = floor(($datediff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($datediff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
$hours = floor($datediff / 3600);
$minutes = floor($datediff / 60);
$seconds = floor($datediff);
Here's the full pastebin https://pastebin.com/tzBN2gZW
Any thoughts on that?
Thanks
The error is occured because you don't reduce datediff after days counting. But I think it's more suitable to use DateTime objects for such calculating
$today = time();
$createdday = mysql_to_unix($ptime);
$today_d = new DateTime();
$today_d->setTimestamp($today);
$createdday_d = new DateTime();
$createdday_d->setTimestamp($createdday);
print_r($today_d->diff($createdday_d));
demo

Difference in hours between 2 particular days - PHP [duplicate]

This question already has answers here:
How do I find the hour difference between two dates in PHP?
(10 answers)
Closed 6 years ago.
I have two date objects say 10/01/2016 00:00:00 to 18/01/2016 08:00:00. I want to check whether these 2 dates lies between friday 12 AM to Sunday 12 Am then how to find difference in hours ?
PHP code -
$t1 = StrToTime($date2);
$t2 = StrToTime($date1);
$diff = $t1-$t2;
$hours = abs($diff / ( 60 * 60 ));
I am badly stuck in this. Please someone help.
Thanks in Advance.
If I understand your question correctly you want to test if the date is either a friday or a saturday, with no relevance how far they are away from each other.
You can use the format() function of the DateTime object to get the weekday of the date, then test if it is a friday (5) or a saturday (6):
$t1 = DateTime::createFromFormat("d/m/Y H:i:s", "10/01/2016 00:00:00");
$t2 = DateTime::createFromFormat("d/m/Y H:i:s", "18/01/2016 08:00:00");
function testDate($date) {
$d = $date->format("N");
if ($d == 5) return true;
if ($d == 6) return true;
return false;
}
$d1 = testDate($t1);
$d2 = testDate($t2);
var_dump($d1, $d2);
// Result:
// bool(false)
// bool(false)
Then you can use the DateTime::diff() function to get the difference:
$diff = $t1->diff($t2);
var_dump($diff);
Result:
object(DateInterval)#3 (15) {
["y"]=>
int(0)
["m"]=>
int(0)
["d"]=>
int(8)
["h"]=>
int(8)
["i"]=>
int(0)
["s"]=>
int(0)
["weekday"]=>
int(0)
["weekday_behavior"]=>
int(0)
["first_last_day_of"]=>
int(0)
["invert"]=>
int(0)
["days"]=>
int(8)
["special_type"]=>
int(0)
["special_amount"]=>
int(0)
["have_weekday_relative"]=>
int(0)
["have_special_relative"]=>
int(0)
}
So, 8 days ($diff['days']) and 8 hours ($diff['h'])
You can use:
$date1 = "2016-08-14 00:00:00"; //more higger day
$date2 = "2016-08-13 00:00:00";
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
$minuts = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60));
printf("%d years, %d months, %d days, %d hours, %d minuts\n, %d seconds\n", $years, $months, $days, $hours, $minuts, $seconds);
Output: 0 years, 0 months, 1 days, 0 hours, 0 minuts , 0 seconds
Now, you can do:
$date1 = "2016-08-14 00:00:00"; //more higger day
$date2 = "2016-08-13 00:00:00";
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
echo "Totals hours: ".($days*24 + $hours);
Returns 24 (and you can multiply the years, months and so on)

If hours over 24 don't reset

I currently made a function which tells from timestamp the hours, minutes and seconds left. But when the item is ending in more than 24 hours it should show example 48 hours and not resetting it. How can i do this?
I have days already, but i should be only in hours, minutes and seconds, ex.
76 hours, 20 minutes & 20 seconds.
$time = strtotime($row['start_date']) - time();
$days = floor($time / (60 * 60 * 24));
$time -= $days * (60 * 60 * 24);
$hours = floor($time / (60 * 60));
$time -= $hours * (60 * 60);
$minutes = floor($time / 60);
$time -= $minutes * 60;
$seconds = floor($time);
$time -= $seconds;
$hours = ($hours<10) ? "0" . $hours : $hours;
$minutes = ($minutes<10) ? "0" . $minutes : $minutes;
$seconds = ($seconds<10) ? "0" . $seconds : $seconds;
$days = floor($time / (60 * 60 * 24));
$time -= $days * (60 * 60 * 24);
Your code removes the day part from the timestamp, that's why.

PHP < 5.3 => Difference in hours between two dates

In a current project I have hit a wall because I need to compare two dates and find the difference between the dates (in hours), this would be easy if the server was >= 5.3, can someone help me?
I have the difference in timestamp
$diff = abs(strtotime($date1)-strtotime($date2)
but I don't know what to do next...
Thank you.
In your code $diff is the difference in seconds. You can convert the seconds to hours like this:
$hours = floor($diff / (60 * 60));
Edit: To get minutes and seconds:
$minutes = floor(($diff - $hours * 60 * 60) / 60);
$seconds = floor($diff - $hours * 60 * 60 - $minutes * 60);
Try this
echo round((strtotime($date1) - strtotime($date2))/(60*60));
In your code $diff is in seconds.
There are 3600 seconds in an hour, so:
$date1 = "2014-07-15";
$date2 = "2014-07-17";
$diff_seconds = abs(strtotime($date1)-strtotime($date2));
$diff_hours = $diff_seconds/3600;
echo $diff_seconds.' '.$diff_hours;

Categories