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');
Related
I want to get month between two dates. I've googled a lot but still don't have the answer.
$date1 = new \DateTime("2021-07-20");
$date2 = new \DateTime("2021-08-15");
$interval = $date2->diff($date1);
dd($interval->m); // 0 months.
Above, counting $date1 and $date2 is less than 30 days so it returns 0 months which is correct. But now I want to get the result 2. It's July and August. Is there any way to get that?
Thanks
I tried a few approaches to tackle this.
The easiest method I've found that works for all use cases involves using regular math rather than date functions. The date diff methods I've tried all gave incorrect answers at some point because of the differing number of days in a month.
function getCoveredMonths(DateTime $date1, DateTime $date2): int {
$earlierDate = min($date1, $date2);
$laterDate = max($date1, $date2);
$earlierMonthValue = $earlierDate->format("Y") * 12 + $earlierDate->format("n");
$laterMonthValue = $laterDate->format("Y") * 12 + $laterDate->format("n");
return $laterMonthValue - $earlierMonthValue + 1;
}
// Your example
$date1 = new DateTime("2021-07-20");
$date2 = new DateTime("2021-08-15");
var_dump(getCoveredMonths($date1, $date2));
// 2: July + August
// Same month
$date3 = new DateTime("2021-07-15");
$date4 = new DateTime("2021-07-20");
var_dump(getCoveredMonths($date3, $date4));
// 1: July
// Day overflow test (01-30 + 1 month = 02-30 which overflows to 03-02)
$date5 = new DateTime("2021-01-30");
$date6 = new DateTime("2021-03-01");
var_dump(getCoveredMonths($date5, $date6));
// 3: January + February + March
// Year pass test
$date7 = new DateTime("2021-12-20");
$date8 = new DateTime("2022-02-15");
var_dump(getCoveredMonths($date7, $date8));
// 3: December + January + February
// Wrong order ($date2 is before $date1)
$date9 = new DateTime("2021-10-20");
$date10 = new DateTime("2021-08-15");
var_dump(getCoveredMonths($date9, $date10));
// 3: August + September + October
<?php
$origin = new \DateTime('2020-08-01');
$target = new \DateTime('2021-12-01');
$interval = $origin->diff($target);
echo (($interval->format('%y') * 12) + $interval->format('%m'));
// output is 16 months
?>
If you want to calculate with the full months, you have to calculate from the 1st of the start month to the 1st after the end of the month.
$date1 = new \DateTime("2021-07-20");
$date2 = new \DateTime("2021-08-15");
//
$date1->modify('first day of this Month'); //2021-07-01
$date2->modify('first day of next Month'); //2021-09-01
$interval = $date1->diff($date2);
$countMonth = $interval->m; //2
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");
I have a date outputted from the database:
$deadline = $row['DEADLINE'];
When I print $deadline it returns: 2015-05-03 18:00:00
Now I want a way to find each day 24 hours before up until todays date and then use each of those values to insert in a new table in the database.
So in this case I want:
2015-05-03 18:00:00
2015-05-02 18:00:00
2015-05-01 18:00:00
2015-04-30 18:00:00
2015-04-29 18:00:00
To be inserted into the database 5 new rows. So I know I need todays date:
$now = date("Y-m-d H:i:s");
Then I have the date of the deadline:
$deadline = $row['DEADLINE'];
I can then find the difference in days:
$dateDiff = ($deadline - $now)/(24*60*60);
This gives me a figure (in this case it will be 5). Then could I use this figure to display the date and time for 5 days prior?
Not sure how I would insert them all though. Your help would be appreciated.
Use DateTime object, really easiest way :
PHP :
$now = new datetime();
$sub = new datetime();
for($i=0;$i<5;$i++)
{
$sub->sub(new DateInterval('P1D'));
echo $sub->format('Y-m-d H:i:s').'<br />';
echo $sub->diff($now)->format('%a days').'<br />';
}
OUTPUT :
2015-04-29 14:18:09
1 days
2015-04-28 14:18:09
2 days
2015-04-27 14:18:09
3 days
2015-04-26 14:18:09
4 days
2015-04-25 14:18:09
5 days
This isent perfect, but it will probably get you started
Try Using Datetime http://php.net/manual/en/class.datetime.php
Or Carbon/Carbon Package: https://github.com/briannesbitt/Carbon
$datetime = new DateTime("2015-04-03 18:00:00");
$now = new DateTime('NOW');
$difference = $datetime->diff($now);
echo '<pre>';
for($i = 1; $i <= $difference->days; $i++)
{
if($difference->invert){
var_dump (
$temp = $datetime
->modify('-1 day')
->format('Y-m-d H:i:s')
);
}else{
var_dump (
$temp = $datetime
->modify('+1 day')
->format('Y-m-d H:i:s')
);
}
}
Consider two dates '2011-01-01' and '2011-01-02'.I want to calculate the number of days between these two dates.The result what I want is 2 days(i.e including both start and end date).I used several date functions in php and mysql,but all returns 1 as the answer.
$date11 = '2011-01-01';
$date22 = '2011-01-02';
$dt1 = new DateTime($date11);
$dt2 = new DateTime($date22);
$diff = $dt2->diff($dt1);
echo $diff->format("%a");
Any solution in php or mysql would be grateful.Thanks in advance.
If you want to get 2 days for this interval you can just +1 to the result, because none of the combinations of functions will give 2 days.
$date11 = strtotime($date11);
$date22 = strtotime($date22);
$diff = $date22 - $date11;
$diff_in_days = floor($diff/(60*60*24)) + 1;
Example
You can use DateTime and DateInterval and do you add 1 day?
DateTime
DateInterval
<?php
$d1 = new DateTime('2011-01-01');
$d2 = new DateTime('2011-01-02');
$d2->add(new DateInterval('24h'));
$interval = $d2->diff($d1);
$interval->format('%d Days');
$interval->format('%h Hours');
?>
in mysql:
select abs(datediff('2011-01-01','2011-01-02'))+1 as difference;
+------------+
| difference |
+------------+
| 2 |
+------------+
1 row in set (0.00 sec)
datediff() returns the number of days between the two dates, abs() makes sure we don't get negative values when switching first and second date and the final +1 because you wanted start and enddate to be included in the number.
Use DateDiff. The online docs have this answer as the first example:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$datetime2->modify("+1 days");
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days'); // output
Another way to do this:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
$diff = $interval->days + 1;
echo $diff . " days.";
How can I get the total hour from two given time with "HH:mm:ss" format?
Here is my code:
$start = 01:00:00 PM;
$end = 02:00:00 PM;
Expected Output:
$total = 1;
You can try like this:
$start = new DateTime('01:00:00 PM');
$end = new DateTime('02:00:00 PM');
$diff = $start->diff($end);
echo $diff->format('%H');
You can get more info: http://in1.php.net/manual/en/datetime.diff.php