I would like to calculate days based on two dates but it's given incorrect count of days.
I have 2 variables
$date=$date[1]; Is coming from Database
$now =date('d/m/Y'); Current date
$date variable value is(comes from db) 05/03/2019
I used following script for count days based on two dates but its return 120 days.
Method 1
$date=$date[1]; `Is coming from Database`
$now =date('d/m/Y');
$date1 = new DateTime($now);
$date2 = new DateTime($date);
$diff = $date2->diff($date1)->format("%a");
Method 2
$datetime1 = new DateTime($now);
$datetime2 = new DateTime($date);
$difference = $datetime1->diff($datetime2);
echo 'Difference: '.$difference->y.' years, '
.$difference->m.' months, '
.$difference->d.' days';
print_r($difference);
But it's returns wrong days in following output
Difference: 0 years, 4 months, 120 daysDateInterval Object
(
[y] = 0
[m] = 4
[d] = 0
[h] = 0
[i] = 0
[s] = 0
[invert] = 0
[days] = 120
)
Why this given wrong output of days ?
If you use 'd/m/Y' format you should use createFromFormat function to convert string date to date object. For example:
$date1 = new \DateTime();
$date2 = \DateTime::createFromFormat('d/m/Y', '05/03/2019');
$diff = $date2->diff($date1)->format("%a");
Related
I want to count the total number of days between two date.
Note: I am not talking about the difference between two date
I found lots of answer on google and Stack Overflow. I am sharing here
$now = time(); // or your date as well
$your_date = strtotime("2018-06-01");
$datediff = $now - $your_date;
echo round($datediff / (60 * 60 * 24));
Output is 9
$now = strtotime("2018-06-09");
$your_date = strtotime("2018-06-01");
$datediff = $now - $your_date;
echo $numberDays= round($datediff / (60 * 60 * 24));
Output is 8
$dStart = new DateTime('2018-06-01');
$dEnd = new DateTime('2018-06-09');
$dDiff = $dStart->diff($dEnd);
echo $dDiff->days;
Output is 8
$date1=date_create("2018-06-01");
$date2=date_create("2018-06-09");
$diff=date_diff($date1,$date2);
echo $diff->format("%a");
Output is 8
$startTimeStamp = strtotime("2018-06-01");
$endTimeStamp = strtotime("2018-06-09");
$timeDiff = abs($endTimeStamp - $startTimeStamp);
$numberDays = $timeDiff/86400; // 86400 seconds in one day
// and you might want to convert to integer
echo $numberDays = intval($numberDays);
Output is 8
The correct answer for me is the first one 9 because it displays the total number of days from 1 to 9, not a difference.
I just want to know how to get the count because I don't want to use the time() because it's taking the current date.
For example: If I select the first date 2018-05-01 and my second date in 2018-05-31. So total days is 31, not 30.
Example:
I am working on a project which is related to the working days and salary. So employee will select the date. for example. I am the employee and in the last month May, I select the date 2018-05-01 and 2018-05-31 so I have to display the total working days.So the according to the date I worked 31 days and using above code in the question I am getting 30 days.
Hope you understand my issue. Would you help me out in this?
You can try this-
$dateOne = new DateTime("01-01-2018");
$dateTwo = new DateTime("09-06-2018");
echo $diff = $dateTwo->diff($dateOne)->format("%a");
Output- 159
Use the DateTime class and compare them via diff() method. $date1->diff($date2). The output will give you an accurate result of days:
$date1 = new DateTime('2018-01-01');
$date2 = new DateTime('2018-02-15');
print_r($date1->diff($date2));
Output:
DateInterval Object
(
[y] => 0
[m] => 1
[d] => 14
[h] => 0
[i] => 0
[s] => 0
[f] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 45
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
Output : 272 days.
You can try this:
<?php
$start_date = "2020-12-24";
$end_date = "2020-12-31";
$dateDiff = strtotime($end_date) - strtotime($start_date);
$numOfDays = $dateDiff / 86400;
echo $numOfDays;
?>
Output: 7
First compile and print $now and write down out put after some minutes 2nd time compile and print $now you will ge a difference
here i got your solution in first program php time(); function is dynamically change as per current time in millisecond (In PHP you can simply call time() to get the time passed since January 1 1970 00:00:00 GMT in seconds) here you are using round function to get rounded value after 12pm it your calculation will increase more then 8.5 and you get a rounded value which is 9
here brief explanation Getting unix timestamp in milliseconds in PHP5 and Actionscript3
in rest of program you are taking static time or custom time which are not effected by system time
$now = time(); // or your date as well
$your_date = strtotime("2018-06-01");
$datediff = $now - $your_date;
echo round($datediff / (60 * 60 * 24));
try above program before 12pm maybe you got your answer
I have a date time in 'Y-m-d H:i:s', and i tried to substract the now date with the defined date +1 day to get remaining time in hours, minutes and seconds:
$time = '2017-10-05 14:54:03';
$now = date('Y-m-d H:i:s');
$endTransaction = date('Y-m-d H:i:s', strtotime($time. ' + 1 day'));
$dteDiff = $endTransaction - $now;
echo $dteDiff;
but i always get 0 as the result
You are doing it wrong. The date function returns string so PHP is not able to compare anything. Try with the DateTime class instead. Its diff method returns the DateInterval object with some public properties, like the days property among others, which is the positive integer number (rounded down) of days between two dates:
$now = new \DateTime();
$endTransaction = (new \DateTime('2017-12-05 14:54:03'))->modify('+1 day');
$diff = $endTransaction->diff($now);
printf(
'Difference in days: %d, hours: %d, minutes: %d, seconds: %d',
$diff->days,
$diff->h,
$diff->m,
$diff->s
);
You probably need to use this date_diff
$time = '2017-10-05 14:54:03';
$now = date_create(date('Y-m-d H:i:s'));
$endTransaction = date_create(date('Y-m-d H:i:s', strtotime($time. ' + 1 day')));
$dteDiff = date_diff($now, $endTransaction);
$date = new DateTime($dteDiff);
$result = $date->format('Y-m-d H:i:s');
According to above mentioned description please try executing following code snippet as a solution to it.
$time = '2017-10-05 14:54:03';
$now = strtotime(date('Y-m-d H:i:s'));
$endTransaction = strtotime(date('Y-m-d H:i:s', strtotime($time. ' + 1 day')));
$dteDiff = ($endTransaction - $now)/(24*60*60);
echo round($dteDiff);
$endTransaction and $now are strings.
$time = '2017-10-05 14:54:03';
$now = date('Y-m-d H:i:s');
$endTransaction = date('Y-m-d H:i:s', strtotime($time. ' + 1 day'));
echo($endTransaction."\n");
echo($now."\n");
It prints:
2017-10-06 14:54:03
2017-10-05 11:45:39
The subtraction is not a valid operation for strings. It can handle only numbers. The strings above are converted to numbers. The conversion uses only the leftmost digits present in the string, until it reaches the first character that is not a digit.
Both strings above produce 2017 when they are converted to numbers and their difference is, of course, 0.
The easiest way to work with dates in PHP is to use the DateTime and its related classes.
// Convert the input string to a DateTime object
$then = new DateTime('2017-10-05 14:54:03');
// Add 1 day
$then->add(new DateInterval('P1D'));
// Get the current date and time
$now = new DateTime('now');
// Compute the difference; it is a DateInterval object
$diff = $now->diff($then);
// Display the dates and the difference
echo('Then: '.$then->format("Y-m-d H:i:s\n"));
echo('Now : '.$now->format("Y-m-d H:i:s\n"));
echo('Remaining: '.$diff->format("%R%a days, %h hours, %i minutes, %s seconds.\n"));
The output:
Then: 2017-10-06 14:54:03
Now : 2017-10-05 12:36:25
Remaining: +1 days, 2 hours, 17 minutes, 38 seconds.
I try to write a script that can calculate the difference between two dates, in days, and the diff does not behave as I need to.
More specific, lets say we have the following two date/times:
2015-03-18 23:00
2015-03-19 02:00
The actual time difference is four hours, and in this terms the diff works fine !
But what I like to know is, if the calendar date has been change, and what is the actual difference.
So in the example above, the calendar dates having 1 day difference.
In the following example
2015-03-18 23:00
2015-03-21 02:00
I have three days difference. So how can I calculate this date difference ?
At the moment I use the following code:
$datetime1 = new DateTime('2009-10-11 23:30');
$datetime2 = new DateTime('2009-10-12 02:30');
$interval = $datetime1->diff($datetime2);
echo "<pre>";
print_r($interval);
echo "</pre>";
and the result is the following:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 3
[i] => 0
[s] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 0
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
Is there any idea ? Thanks ...
A really simple solution
Notice: this will only work if the days are in the same month.
$datetime1 = new DateTime('2015-03-18 23:00');
$datetime2 = new DateTime('2015-03-21 02:00');
$difference = $datetime2->format('d') - $datetime1->format('d'); //3
Clean solution
You could remove everything from the date, but the year, month and day and use diff() as you already did.
$datetime1 = new DateTime('2015-03-18 23:00');
$datetime2 = new DateTime('2015-03-21 02:00');
$datetime1modified = new DateTime($datetime1->format('Y-m-d'));
$datetime2modified = new DateTime($datetime2->format('Y-m-d'));
$difference = $datetime1modified->diff($datetime2modified)->d; //3
you surely could use a function like this:
$time1 = strtotime("2008-12-13 10:42:00");
$time2 = strtotime("2010-10-20 08:10:00");
$diff = $time2-$time1;
// the difference in int. then you can divide by 60,60,24 and
// so on to get the h:m:s out of it
or if you more into the build in php functions then something like this might suit your needs:
$date_a = new DateTime('2010-10-20 08:10:00');
$date_b = new DateTime('2008-12-13 10:42:00');
$interval = date_diff($date_a,$date_b);
echo $interval->format('%h:%i:%s');
best regards.
What you can do is remove the Time from each of the dates and then calculate.
example:
$datetime1 ='2009-10-11 23:30';
$datetime2 = '2009-10-12 02:30';
$date1_explode = explode($datetime1,' ');
$date1_explode = explode($datetime1,' ');
$date = $date1_explode[1];
$date = $date2_explode[1];
$date1 = new DateTime($datetime1);
$date2 = new DateTime($datetime1);
$interval = $date1->diff($date2);
echo "<pre>";
print_r($interval);
echo "</pre>";
If you don't care about the hours and want to know only if the date changed you can try to diff the dates after you set them to the same hour:
$datetime1 = new DateTime('2009-10-11 23:30');
$datetime2 = new DateTime('2009-10-12 02:30');
// Work on duplicates to not change the original objects if they are needed later
$date1 = clone $datetime1;
$date2 = clone $datetime2;
// Set the same hour on both $date1 and $date2
$date1->setTime(0, 0, 0);
$date2->setTime(0, 0, 0);
// Now you can simply compare $date1 to $date2 to see if they are equal
if ($date1 == $date2) {
echo('$datetime1 and $datetime2 are on the same date.');
} else {
echo('$datetime1 and $datetime2 are on different dates.');
}
// Or you can compute the difference
$diff = $date2->diff($date1);
// and format it as you like
echo('There are '.$diff->format('%d').' days between '.$date1.' and '.$date2);
$date1 = new DateTime("2014-02-28");
$date2 = new DateTime("2014-04-02");
$interval = $date1->diff($date2);
This results in 1 month and 5 days.
I expected it to result in 1 month and 2 days, where the one month is to 2014-03-31 and two days to get up to 2014-04-02
It seems like the month is set to 2014-03-28, and then 5 days to get to 2014-04-02.
How can I get the result I expect?
$date1 = new DateTime("2014-02-28");
$date2 = new DateTime("2014-04-02");
$interval = $date1->diff($date2);
echo $interval->format('%m'), ' months and ', $interval->format('%a') % 31, ' days';
see this code snippet ,use strtotime()
<?php
$result= strtotime("2014-04-02")-strtotime("2014-02-28");
$resul1=$result/(3600*24);
echo $resul1;
?>
Demo here: http://codepad.org/fwKY75sX
I have to find the days until an expiration date.
I tried to use diff method of DateTime class.
$dataexp = 2013-11-06 00:00:00 ;
$now = 2013-11-05 13:00:00 ;
$dtn = new DateTime('now');
$dte = new DateTime($dataexp);
$diff = $dtn->diff($dte);
$days = sprintf("%01d", $diff->days);
$days ---> display 1
My problem is if the dataexp is in the past of 1 day the result of diff is 1 and not -1
$dataexp = 2013-11-04 00:00:00 ;
$now = 2013-11-05 13:00:00 ;
$dtn = new DateTime('now');
$dte = new DateTime($dataexp);
$days = sprintf("%01d", $diff->days);
$days ---> display 1
What method could I use to get what I want? (-1 days)? Thanks
See DateInterval::format(), specifically the r format character.
echo $diff->format('%r%d');