Calculate the difference between two dates in PHP? - php

I want calculate the difference between two dates in Javascript.
Result should be X years, Y months, Z days
I run below code in PHP but result is different. Which is problem in here?
PHP CODE 1
// Declare and define two dates
$date1 = strtotime("2010-04-01");
$date2 = strtotime("2019-06-30");
// Formulate the Difference between two dates
$diff = abs($date2 - $date1);
// To get the year divide the resultant date into
// total seconds in a year (365*60*60*24)
$years = floor($diff / (365*60*60*24));
// To get the month, subtract it with years and
// divide the resultant date into
// total seconds in a month (30*60*60*24)
$months = floor(($diff - $years * 365*60*60*24)
/ (30*60*60*24));
// To get the day, subtract it with years and
// months and divide the resultant date into
// total seconds in a days (60*60*24)
$days = floor(($diff - $years * 365*60*60*24 -
$months*30*60*60*24)/ (60*60*24));
// To get the hour, subtract it with years,
// months & seconds and divide the resultant
// date into total seconds in a hours (60*60)
$hours = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24)
/ (60*60));
// To get the minutes, subtract it with years,
// months, seconds and hours and divide the
// resultant date into total seconds i.e. 60
$minutes = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24
- $hours*60*60)/ 60);
// To get the minutes, subtract it with years,
// months, seconds, hours and minutes
$seconds = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24
- $hours*60*60 - $minutes*60));
// Print the result
printf("%d years, %d months, %d days, %d hours, "
. "%d minutes, %d seconds", $years, $months,
$days, $hours, $minutes, $seconds);
?>
Result is 9 years, 3 months, 2 days
HOWEVER. if I run below code, Result is 9 years, 2 months, 29days
PHP CODE 2
//差を求める日時の変数を作成
$dateTime1 = '2010-04-01';
$dateTime2 = '2019-06-30';
//DateTimeクラスで2つの日時のオブジェクトを作成
$objDatetime1 = new DateTime($dateTime1);
$objDatetime2 = new DateTime($dateTime2);
//diff()メソッドで2つの日時のオブジェクトから
//ふたつの日付の差をあらわす DateInterval オブジェクトを生成する
$objInterval = $objDatetime1->diff($objDatetime2);
//$objInterval(DateInterval オブジェクト)をformat()メソッドで日時を出力
//%Rは + や - の符号を出力するオプションです
echo $objInterval->format('%R%Y').'year<br/>'; //年
echo $objInterval->format('%R%M').'month<br/>'; //月
echo $objInterval->format('%R%D').'day<br/>'; //日
?>
9 years, 3 months, 2 days and 9 years, 2 months, 29days
what is correct?

A date difference by itself can only be precise to the Day. If you go up to the months and years, then the results will be inevitably false without a date reference.
You can say
9 years, 2 months, 29days till X date
and the reference would be today.
If you take the same period(Year, month, day), and use the date 2019-03-01 as reference, you would have the wrong result.
Here is an example:
$objDatetimeSource1 = new DateTime('2010-04-01');
$objDatetimeSource2 = new DateTime('2019-06-30');
$interval = $objDatetimeSource1->diff($objDatetimeSource2);
$objDatetime1 = new DateTime('2020-02-28');
$objDatetime2 = new DateTime('2020-03-01');
$objDatetime1->add($interval); will give 2029-05-27 00:00:00 and
$objDatetime1->add($interval); will give 2029-05-30 00:00:00 with 3 days difference between them, when, from the dates 2020-02-28 and 2020-03-01, you can clearly see that there is only 2 days difference.

Related

Remaining time beetwen two time

i have time data in database, i'm recording time with time() function (value 1551866538) on database, and i want write php remaining time for 1 day but as 24 hour. I try date_diff() function but i get error every time, thanks for help.
$mysqltime = $data->registerdate; // 1551866538
$now = time();
$remainingtime = $now - $mysqltime; // I want show as hour
You can also use The DateTime class as below:
$today = new DateTime('now');
$tomorrow = new DateTime(date('Y-m-d H:i:s', '1551866538'));
$difference = $today->diff($tomorrow);
echo $difference->format('%h hours %i minutes remaining');
Output
1 hours 42 minutes remaining
<?php
// Declare and define two dates
$date1 = strtotime("2016-06-01 22:45:00");
$date2 = strtotime("2018-09-21 10:44:01");
// Formulate the Difference between two dates
$diff = abs($date2 - $date1);
// To get the year divide the resultant date into
// total seconds in a year (365*60*60*24)
$years = floor($diff / (365*60*60*24));
// To get the month, subtract it with years and
// divide the resultant date into
// total seconds in a month (30*60*60*24)
$months = floor(($diff - $years * 365*60*60*24)
/ (30*60*60*24));
// To get the day, subtract it with years and
// months and divide the resultant date into
// total seconds in a days (60*60*24)
$days = floor(($diff - $years * 365*60*60*24 -
$months*30*60*60*24)/ (60*60*24));
// To get the hour, subtract it with years,
// months & seconds and divide the resultant
// date into total seconds in a hours (60*60)
$hours = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24)
/ (60*60));
// To get the minutes, subtract it with years,
// months, seconds and hours and divide the
// resultant date into total seconds i.e. 60
$minutes = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24
- $hours*60*60)/ 60);
// To get the minutes, subtract it with years,
// months, seconds, hours and minutes
$seconds = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24
- $hours*60*60 - $minutes*60));
// Print the result
printf("%d years, %d months, %d days, %d hours, "
. "%d minutes, %d seconds", $years, $months,
$days, $hours, $minutes, $seconds);
?>
Output:
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Reference
$mysqltime = $data->registerdate; // 1551866538
$now = time();
$remainingtime = $now - $mysqltime;
echo $hours = round($remainingtime / (60 * 60)); // hours

Get time past between two dates [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 4 years ago.
I used the following code to find out the time past between two dates:
$date1 = "1900-00-00";
$date2 = "2000-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));
printf("%d years, %d months, %d daysn", $years, $months, $days);
But this prints 100 years, 0 months, 24 daysn, instead of 100 years, 0 months, 0 daysn
What is going on?
You can use the DateTime::diff method of the DateTime class to get the difference between two dates. You don't need to calculate the difference yourself:
<?php
$date1 = "1900-00-00";
$date2 = "2000-00-00";
$dt1 = new DateTime($date1);
$dt2 = new DateTime($date2);
$diff = $dt1->diff($dt2);
printf("%d years, %d months, %d days", $diff->y, $diff->m, $diff->d);
//output: 100 years, 0 months, 0 days
You can also use the procedural style to get and output the difference in two lines:
$diff = date_diff(new DateTime("1900-00-00"), new DateTime("2000-00-00"));
printf("%d years, %d months, %d days", $diff->y, $diff->m, $diff->d);
//output: 100 years, 0 months, 0 days
demo: https://ideone.com/rosaJJ
Here is the easiest solution :-
$date1 = "1900-00-00";
$date2 = "2000-00-00";
$expDate = date_create($date2);
$todayDate = date_create($date1);
$diff = date_diff($todayDate, $expDate);
printf("%d years, %d months, %d days", $diff->y, $diff->m, $diff->d);
You will get your expected result .

warranty time calculation of a product is not working using php

I am working a stuff with calculate warranty time of a product.A product have warranty 2 years 0 months.These product is purchased in these date '2015-07-24'.I want to calculate remaining warranty time of these product.I got the remaining warranty as '1 Years, 12 Months, 2 Days' using these below code.But actual warranty is '2 years' and remaining warranty is '1 Years, 12 Months, 2 Days'.Remaining warranty period is greater then actual warranty.Product is purchased before before 4 days ago,in the date '2015-07-24'.
Used these code
$date3= '2015-07-24';
$warranty_year= 2;
$warranty_month = 0;
$newDate = date('Y-m-d', strtotime($date3. " + {$warranty_year} year"));
$newDate2 = date('Y-m-d', strtotime("+{$warranty_month} months", strtotime($newDate)));
$current_date=date("Y-m-d");
$date1Timestamp = strtotime($current_date);
$date2Timestamp = strtotime($newDate2);
$diff = $date2Timestamp - $date1Timestamp;
$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));
printf("%d Years, %d Months, %d Days\n", $years, $months, $days);
Anybody help me?
$warranty= date of purchase + 2years;
//dateof purchase write it as your variable from db
$date1=date_create( $warranty);// this is your warranty time after 2 years
$date2=date_create(date("Y-m-d H:i:s")) ;//this gives current time
$diff=date_diff($date1,$date2);
$noOfDays=$diff->format("%a days %H:%i:%s Hours");
echo $noOfDays;

How to get date difference in php? [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
In php, if you have a string in the date/time format example "2013-09-08 00:25:31", how can you compare that with the current time, and get the difference in number of days?
Thanks.
you should check DateTime::diff
you may do it like
$now = new DateTime();
$prev = new DateTime('2013-09-08');
$interval = $now->diff($prev);
echo $interval->format('%R%a days');
You can use strtotime() to convert two dates to unix time and then calculate the number of seconds between them. From this it's rather easy to calculate different time periods.
$date1 = "2007-03-24";
$date2 = "2009-06-26";
$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));
printf("%d years, %d months, %d days\n", $years, $months, $days);
You check this link for more answers.

return difference of two dates in PHP [duplicate]

This question already has answers here:
Get interval seconds between two datetime in PHP?
(8 answers)
Closed last year.
I need to compare a past/future date with current date in PHP and present the difference in a "4 hours until", or "2 days 3 hours until" or "5 hours ago" format. Optionally in a "-4h" (which is bad) or "4h" (which is good) format.
So in a example:
x = $expiry_date - $todays_date
If the result is positive, eg. $expiry_date is 4 hours in the future, x = "4 hours to go", or "4 hrs". If the result was negative, for example "4 hours ago" or "-4hrs".
Any other, similarly sounding result formats are fine.
Any help please?
Refer Dates diff in PHP
$date1 = "2013-08-11";
$date2 = "2012-07-12";
$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));
printf("%d years, %d months, %d days\n", $years, $months, $days);
Or
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
//or simply
//echo "difference " . $interval->days . " days ";
You can do that using the Carbon library.
It would go something like this:
$now = Carbon::now();
$then = Carbon::create(2013, 8, 8, 23, 26, 11);
echo $now->diffForHumans($then);
The output will be a human readable string just like what you're looking for.
See this question.
The variable will contain the difference in seconds, which you can then divide as required to get minutes/hours/days etc. E.g. divide by 60 to get minutes, 60 again to get hours etc. Then do some simple value checking to format as required.

Categories