I am trying to find difference between two dates.
It simple does not display anything , empty page shown for the date difference.
below is the code that i am using.
<?
$sql = $Db1->query("SELECT * FROM table LIMIT 5");
while($row = $Db1->fetch_array($sql)) {
$datetime1 = date("m-d-Y",mktime(0,0,$row['rec'],1,1,1970));
$datetime2 = date("m-d-Y",mktime(0,0,$row['send'],1,1,1970));
echo "$datetime1";
echo "$datetime2";
$diff=date_diff($datetime1,$datetime2);
echo $diff->format("%R%a days");
echo "$diff";
}
?>
use this function
$date1 = $row['rec'];
$date2 = $row['send'];
$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 created a function that i use you can change it to as per your requirement.
leave date two empty if you want to find difference b/w current date and given date.
function dayspassed($dategiven, $dategiven2){
if(empty($dategiven2)){
$currentdate = date('Y-m-d');
}else{
$currentdate = $dategiven2;
}
$currentdate = strtotime($currentdate);
$dategiven = explode(" ", $dategiven);
$dategiven = $dategiven['0'];
$yourdate = strtotime($dategiven);
$datediff = $yourdate - $currentdate;
return $difference = floor($datediff/(60*60*24));
}
I have the following function:
function get_date_difference_in_days($timestamp1, $timestamp2) {
$inputSeconds = abs($timestamp1 - $timestamp2);
$days = floor($inputSeconds / (24 * 60 * 60));
return $days;
}
The problem with your code is that date_diff works with datetime objects, and you were passing parameters as a string and not as datetime objects.
Looking at the docs clearly states that date_diff is an alias function for DateTime::diff().
So the solution would be:
$row = array("rec" => "2014-12-24", "send" => "2014-12-20");
$datetime1 = DateTime::createFromFormat("Y-m-d", $row["rec"]);
$datetime2 = DateTime::createFromFormat("Y-m-d", $row["send"]);
echo $datetime1->format("Y-m-d");
echo $datetime2->format("Y-m-d");
$diff = $datetime1->diff($datetime2);
echo $diff->format("%r%a");
Related
Hey I am trying to get total months from two dates in php. I have searched about leap year calculations between two dates everywhere on internet but did not find the answer.
If my input is "2019-01-01" to "2019-03-31" then the result i expected is 3 months but result i get is 2 month.
Following is my code .
$date1 = strtotime("2019-01-01");
$date2 = strtotime("2019-02-28");
$diff = abs($date2 - $date1);
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24)
/ (30*60*60*24));
printf("%d months",$months);
where am i going wrong
I Understand Your Problem..
Actually the result you are getting is correct,because day ends at night 12pm and you are checking at day time.So until day ends you cant get complete month.If You add one day and check you will get correctly 3 Months
Below I Made Some Changes to Your Code..
$new_date = date('Y-m-d', strtotime('2019-02-28' . ' +1 day'));
$date1 = strtotime("2019-01-01");
$date2 = strtotime($new_date);
$diff = abs($date2 - $date1);
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24)
/ (30*60*60*24));
printf("%d months",$months);
You can Also Get it by Mysql querying to server
SELECT TIMESTAMPDIFF(MONTH, '2019-01-01', (SELECT DATE_ADD('2019-02-28', INTERVAL 1 DAY))) as month
You can Try it.Hope it Helps.
Your math calculates the number of months between the two dates, exclusive of the ending month.
Add 1 to the total, like so.
$months = (floor(($diff - $years * 365*60*60*24)
/ (30*60*60*24))) +1;
You can try the following:
$date1 = "2019-01-01";
$date2 = "2019-02-28";
$timestamp1 = strtotime($date1);
$timestamp2 = strtotime($date2);
$year1 = date('Y', $timestamp1);
$year2 = date('Y', $timestamp2);
$month1 = date('m', $timestamp1);
$month2 = date('m', $timestamp2);
$diff = (($year2 - $year1) * 12) + ($month2 - $month1);
printf("%d months",$diff);
Try it here: http://sandbox.onlinephpfunctions.com/code/496a08612489977e9e23e8ef3ea07ba991bc5e23
You have to add extra month in your $month variable
$date1 = strtotime("2019-01-01");
$date2 = strtotime("2019-03-31");
$diff = abs($date2 - $date1);
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24)
/ (30*60*60*24)) +1 ;
printf("%d months",$months); //Output : give add one extra
OR
$date1 = date_create('2019-01-01');
$date2 date_create('2019-03-31');
$interval= date_diff($date1, $date1);
echo $interval->format('%m months');
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 need to to find the difference between two date objects in php
I tried this:
if(strtotime($current)>strtotime($LastUpdated))
{
$diff=strtotime($current) - strtotime($LastUpdated);
}
else
{
$diff=strtotime($LastUpdated) - strtotime($current);
}
This gave me junk values.
I also tries this
$diff=date_diff(new DateTime($current),new DateTime($LastUpdated));
This is giving me zero.
How do I go about finding the difference?
The Manual is your friend. - http://pt1.php.net/manual/en/datetime.diff.php
With examples for Object-Oriented and Procedural programming.
pasted from the link above:
OOP:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
Procedural:
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
and the result will be:
+2 days
have fun :)
<?php
$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);
I think this is better for you.
So I have used this method to get the difference between 2 dates.
$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));
Now, lets say that I want to convert the years and months into days. How do I do that?
Using DateTime this is a piece of cake:
$date1 = new DateTime($date1);
$date2 = new DateTime($date2);
$diff = $date1->diff($date2, true);
echo $diff->format('%a') . ' days';
$currentDate = date("d-m-Y");
$date1 = date_create("".$joining_date."");
$date2 = date_create("".$currentDate."");
$diff12 = date_diff($date2, $date1);
$hub_days = $diff12->days;
$months = $diff12->m;
$years = $diff12->y;