I'm facing some problem while calculating the difference between two dates because of Date Format, please help me to fix this issue.
Date 1 - (Format: d/m/Y)
date_default_timezone_set("Asia/Kolkata");
$date1 = date('d/m/Y');
//Output - 20/05/2020
Date 2 - (Format: d/m/Y)
$date2 - 01/27/2020
My Code -
$diff = abs($date2 - $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));
// Print the result
printf("%d years, %d months, %d days", $years, $months, $days);
You don't have to calculate days, month etc manually. There is already DateTime Class available in PHP which you can leverage.
$date1 = DateTime::createFromFormat('d/m/Y', '20/05/2020'); // Use $date1 = new DateTime('NOW'); For Current Time
$date2 = DateTime::createFromFormat('d/m/Y', '25/05/2020');
$interval = $date1->diff($date2);
printf("%d years, %d months, %d days", $interval->y, $interval->m, $interval->d);
Official PHP Documentation: PHP DateTime Class
First you shoud use date format(Format: Y/m/d) and second use strtotime to convert date to seconds because abs function working with numeric values. try following code :
date_default_timezone_set("Asia/Kolkata");
$date1 = strtotime(date('Y/m/d'));
$date2 = strtotime('2020/05/27');
$diff = abs($date2 - $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));
// Print the result
printf("%d years, %d months, %d days", $years, $months, $days);
Output :
0 years, 0 months, 7 days
Related
From Apr 13 to May 12, including the last day, the day difference would be 30 days.
But if I use date_diff, I get only 29 days. How do I include the last day? Do I simply add one more day or is there a more elegant solution?
yes you need to include at the last day like you provided link of 30 day because in php date_diff no issue , please again check your reference link there is also option like include end date in calculation (1 day is added)
PHP date_diff not include last day in calculation default so you need to specify it manual
php date_diff no issue because I have added belo four sample to also calculate date different
note: in below example also need to add one more day if need
#1 example
$date1 = date_create('2022-04-13');
$date2 = date_create('2022-05-12');
$dateDifference = date_diff($date1, $date2)->format('%y years, %m months and %d days');
echo $dateDifference;
#2 example
$date1 = date_create('2022-04-13');
$date2 = date_create('2022-05-12');
$diff=date_diff($date1,$date2);
$months = $diff->format("%m months");
$years = $diff->format("%y years");
$days = $diff->format("%d days");
echo $years .' '.$months.' '.$days;
#3 example
$date1 = date_create('2022-04-13');
$date2 = date_create('2022-05-12');
$diff1 = date_diff($date1,$date2);
print_r($diff1);
#4 example
$date1 = '2022-04-13';
$date2 = '2022-05-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);
I have two times, like this:
$date1 = strtotime("02/12/2019 10:10:54 am");
$date2 = strtotime("02/12/2019 10:11:07 pm");
$diff = abs($date2 - $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));
$minutes = 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 - $minutes*60));
printf(" %d days, %d hours, "
. "%d minutes, %d seconds",
$days, $hours, $minutes, $seconds);
I want to calculate the difference between them, but it's not working.
This is much easier with the DateTime class, which has a diff method which produces a DateInterval object that can output the difference in whatever format you like:
$date1 = new DateTime("02/12/2019 10:10:54 am");
$date2 = new DateTime("02/12/2019 10:11:07 pm");
$diff = $date1->diff($date2);
echo $diff->format('%y years, %m months, %d days, %h hours, %i minutes and %s seconds');
Output:
0 years, 0 months, 0 days, 12 hours, 0 minutes and 13 seconds
Demo on 3v4l.org
Im trying to return the difference between 2 dates, i'm working according to the example found on stackoverflow
My Problem? Im getting completely the wrong results returned, the following code returns 30 years, 0 months, 9 days, when it should obviously be only 7 days or 1 week.
Code follows below:
date_default_timezone_set('America/Los_Angeles');
$pickupDate = '2016-10-13';
$returnDate = 2016-10-20;
$diff = abs(strtotime($pickupDate) - strtotime($returnDate));
$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);
Any input appreciated
just put single quote in return date like $returnDate = '2016-10-20'; and you can use date_diff() function of php like,
$daysdiffernce = date_diff(date_create('2016-10-13'),date_create('2016-10-20'));
echo $daysdiffernce->format("%R%a days");
and this will give exactly +7days answer
First, the code doesn't take into account leap years, varying length of months and things like that.
There is actually a function in php for this, please check the link for details: http://php.net/manual/en/datetime.diff.php , and an example taken:
$datetime1 = new DateTime('2016-10-13');
$datetime2 = new DateTime('2016-10-20');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%y years, %m months, %d days');
Try this, it will give you differ in date, and , time, minutes, hour ,second ,and etc.
date_default_timezone_set('America/Los_Angeles');
$now = '2016-10-13';
$returnDate = '2016-10-20';
$start = date_create($returnDate);
$end = date_create($now);
$diff=date_diff($end,$start);
print_r($diff);
DEMO
From the manual
$pickupDate = new DateTime('2016-10-13');
$returnDate = new DateTime('2016-10-20');
$interval = $pickupDate->diff($returnDate);
echo $interval->format('%R%a days');
http://php.net/manual/en/datetime.diff.php
date_default_timezone_set('America/Los_Angeles');
$pickupDate = '2016-10-13';
$returnDate = '2016-10-20'; //use signle quote same as pickupDate
$diff = abs(strtotime($returnDate) - strtotime($pickupDate)); // change the order
$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);
Thanks
I tried with the below code to find the difference between two dates which is passed through post variable and print, but failed.
$fromdate=$_POST['from_date'];
$todate=$_POST['to_date'];
$date1 = new DateTime($fromdate); //inclusive
$date2 = new DateTime($todate); //exclusive
$diff = $date2->diff($date1);
echo $diff;
Something like this should work for you:
<?php
$_POST['from_date'] = "2014-10-01";
$_POST['to_date'] = "2014-11-02";
$fromdate = $_POST['from_date'];
$todate = $_POST['to_date'];
$diff = abs(strtotime($fromdate) - strtotime($todate));
$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);
?>
Output:
0 years, 1 months, 2 days
I want to subtract one date to another using php and display result in the days - hours - min - sec format. How i do this using php . I tried this using time stamp but it does not give proper values. please give suggestion
For ex : from date 2012-04-27 19:30:56 to date 2012-04-27 19:37:56
i used this code
if(strtotime($history['datetimestamp']) > strtotime($lasttime)) {
$totalelapsed1 = (strtotime($history['datetimestamp'])-strtotime($lasttime));
if($totalelapsed1 > 60 ) {
$sec = $totalelapsed1%60;
$min = round($totalelapsed1/60 , 0);
$minute = $min + $minute;
$second = $sec + $second;
// echo $min. " min " .$sec." sec";
} else {
//echo "0 min " . $totalelapsed1." sec";
$minute = 0 + $minute;
$second = $totalelapsed1 + $second;
}
} else {
$minute = 0 + $minute;
$second = 0 + $second;
// echo "0 min 0 sec";
}
From how to subtract two dates and times to get difference by VolkerK:
You have to use like this:-
<?php
//$now = new DateTime(); // current date/time
$now = new DateTime("2010-07-28 01:11:50");
$ref = new DateTime("2010-07-30 05:56:40");
$diff = $now->diff($ref);
printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);
prints 2 days, 4 hours, 44 minutes
see http://docs.php.net/datetime.diff
edit: But you could also shift the problem more to the database side, e.g. by storing the expiration date/time in the table and then do a query like
... WHERE key='7gedufgweufg' AND expires<Now()
Many rdbms have reasonable/good support for date/time arithmetic.
Link Url:- how to subtract two dates and times to get difference
http://www.webpronews.com/calculating-the-difference-between-two-dates-using-php-2005-11
I suggest to use DateTime and DateInterval objects.
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "days difference ".$interval->d." days ";
read more php DateTime::diff manual
Try using DateTime:diff().
Examples are provided on that page.
This script resolve my issue
$date1 = date("d-m-Y H:i:s",strtotime($date1));
$date2 = date("d-m-Y H:i:s",strtotime($lasttime));
$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);