Difference in UK Dates [duplicate] - php

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 7 years ago.
I am trying to show the difference in days and months between 2 dates
$date1 = $start;
$date2 = $end;
$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);
However this shows as 0 years, 0 months, 0 days
The dates in $start and $end are in the format DD/MM/YYYY

As you have stated within your comment that The dates in $start and $end are in the format DD/MM/YYYY. So you need to first replace that into d-m-Y using str_replace or DateTime::createFromFormat so try using like as
$date1 = str_replace('/','-',$start);
$date2 = str_replace('/','-',$end);
PHP date conversion to strtotime

Related

PHP Date Difference with Custom Format

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

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 .

php compare difference between two dates

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

How to exact difference between 2 dates in php

I want to find difference between 2 dates in php.But I am not getting exact difference.
please help me.
The output I must get as "2 years 0 months 0 days".
But I am getting as "1 years 12 months and 4 days".
where I am wrong?
$createddate="2015-12-24";
//find difference between the dates present-createddate of user
$now = time(); // present time
$your_date = strtotime($createddate);
$difference = abs($now - $your_date);
echo $difference;
// Years, months and days version
$years = floor($difference / (365*60*60*24));
//echo $years;
$months = floor(($difference - $years * 365*60*60*24) / (30*60*60*24));
//echo $months;
$days = floor(($difference - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
//echo $days;
$membersince .= $years.' years '.$months.' months and '.$days.' days';
<?php
$datetime1 = new DateTime();
$datetime2 = new DateTime('2015-12-24');
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%y years, %m months, %d days');
echo $elapsed;
See it in action

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.

Categories