$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
Related
I want to create dynamic expiry date from the today to next 3 months or 6 months or 9 months or 12 months using PHP
I tried but it's not working
$today_date= strtotime(date("Y-m-d"));
$data['expiry_date'] = strtotime(date("Y-m-d", strtotime($valid_months,date("Y-m-d"))));
display code like this
<?php foreach($mydata as value){
$date_expire=date('d M Y',strtotime($value->expiry_date));
$register_date=date_create(date('d M Y ',$value->created));
$expiry_date=date_create($date_expire);
$diff = date_diff($register_date,$expiry_date);
echo $diff->format('%a Days');
}
I am getting output like this
2348824 Days // count remaining days
You could use the DateTime class and the associated methods available such as add and diff
$interval=new DateInterval('P3M');
$now = new DateTime();
$start=new DateTime();
$end=new DateTime( date( DATE_ATOM, strtotime('+1 year') ) );
$end->add( $interval );
while( $start->add( $interval ) <= $end ){
echo $diff = $start->diff( $now )->format('%a') . '<br>';
}
This will output:
92
182
274
366
As far as i understand from your question,i think you just need this.
echo date('d/m/Y', strtotime('+3 months'));
If it Doesn't help , feel free to comment .
i've problem with calculating difference between two dates (including the End Date) using Carbon. Here's the problem:
I'm using this code (source: danharper's answer in https://laracasts.com/discuss/channels/general-discussion/carbon-display-age-in-years-months-day?page=1) :
$dateFrom = new Carbon("2017-01-01");
$dateTo = new Carbon("2017-12-31");
$dateTo = $dateTo->addDay(); //including End Date
echo $dateFrom->diff($dateTo)->format('%y') . " year, <br>";
echo $dateFrom->diff($dateTo)->format('%m') . " month, <br>";
echo $dateFrom->diff($dateTo)->format('%d') . " day <br>";
echo "difference " . $dateFrom->diffInDays($dateTo) . " days <br>";
Scenario 1:
Let's say, $date1 = 2017-01-01 and $date2 = 2017-12-31, then it'll results:
1 year, 0 month, 0 day
difference 365 days
When i'm using date calculator in https://www.timeanddate.com/date/durationresult.html?d1=1&m1=1&y1=2017&d2=31&m2=12&y2=2017&ti=on, it'll results:
It is 365 days from the start date to the end date, end date included
Or 1 year including the end date
They resulting the same answer. BUT:
Scenario 2:
$date1 = 2017-10-01 and $date2 = 2017-12-31, then it'll results:
0 year, 3 month, 1 day
difference 92 days
Using date calculator in https://www.timeanddate.com/date/durationresult.html?d1=1&m1=10&y1=2017&d2=31&m2=12&y2=2017&ti=on, it'll results:
It is 92 days from the start date to the end date, end date included
Or 3 months including the end date
The result in timeanddate.com is exactly 3 months ONLY. Not with 1 day.
I want the result is 3 months (the timeanddate.com's answer).
How can i achieve that answer?
Or, if it can't be achieved, is there any other technique to achieve:
x months y days? (ex: 1 jan 2017 ~ 5 feb 2019 = 25 months, 5 days)
Please help me.
You can change you code like following,
$dateFrom = new Carbon("2017-01-01");
$dateTo = new Carbon("2017-12-31");
$dateTo = $dateTo->addDay(); //including End Date
$days = $dateFrom->diffInDays($dateTo);
$months = $dateFrom->diffInMonths($dateTo);
$years = $dateFrom->diffInYears($dateto);
In you code, you have measured difference many times instead of one time. Use diffInDays(), diffInMonths() and diffInYears() functions to get values of days, months and years between two dates.
Hope you understand.
Scenario 1
$start_date = new DateTime('1 Jan 2017');
$end_date = new DateTime('5 Feb 2019 +1 day');
$difference = $start_date->diff($end_date);
$year_diff = $difference->format('%y');
$months_diff = $difference->format('%m');
$total_months = $months_diff + ($year_diff * 12);
$output = $total_months . ' months ' . $difference->format('%d') . ' days';
// would output 25 months 5 days
Scenario 2
$start_date = new DateTime('2017-10-01');
$end_date = new DateTime('2017-12-31 +1 day');
$difference = $start_date->diff($end_date);
$year_diff = $difference->format('%y');
$months_diff = $difference->format('%m');
$total_months = $months_diff + ($year_diff * 12);
$output = $total_months . ' months ' . $difference->format('%d') . ' days';
// would output 3 months 1 days
I'm having trouble getting "weeks" in DateTime::diff function
Here's my code:
$date1 = new DateTime("2017-05-14");
$date2 = new DateTime("2017-06-14");
$interval = $date1->diff($date2);
echo $interval->m.' '.($interval->m > 1 ? 'months' : 'month');
It worked if I'm going to get the "month" count, but I want to get the weeks before turning it into a month:
We have 4 weeks in a month (4.34524 to be exact from Google Unit Converter), if the difference between start date and the date today exceeds 4 weeks, it should output "1 month" and so on..
Code (Demo):
$date1 = new DateTime("2017-06-1");
$date2 = new DateTime("2017-06-15");
$interval = $date1->diff($date2);
//var_export($interval);
if($interval->m>0){ // check if >= 1 month
echo "{$interval->m} month",($interval->m>1?'s':'');
}else{
$weeks=floor($interval->days/7); // if not yet 1 month, calc weeks
echo "$weeks week",$weeks!=1?'s':'';
}
// output: 2 weeks
Calculate days and then divide by 7 for week.
Try this code :
$date1 = new DateTime("2017-05-14");
$date2 = new DateTime("2017-06-14");
$interval = $date1->diff($date2);
$week = floor($date1->diff($date2)->days/7);
echo $week;
if($week > 4)
{
echo $interval->m.' '.($interval->m > 1 ? 'months' : 'month');
}
you could do this to get weeks and then make the condition:
$daysInAweek = 7;
$weeks = ($interval->days)/$daysInAweek;
if($weeks >= 4) {
echo 'is a month';
}
because a week have 7 days.
How can you count up from a date, May 7, 2016 and just display the number of weeks and days (5 Weeks 1 Day) from that date with the least amount of code?
Use like this
<?php
$date = "May 07, 2011";
$date = strtotime($date);
$week=5;
$day=1;
$days=(5*7)+1;
$date = strtotime("+".$days." day", $date);
echo date('M d, Y', $date);
?>
Use the PHP DateTime class.
$lastDate="2016-10-10";
$date1 = new DateTime("2016-05-07");
$date2 = new DateTime($lastDate);
$difference = $date1->diff($date2);
echo "difference " .floor($difference->days/7)." weeks, ".($interval->days%7)." days ";
Get the number of weeks by dividing and modulus by 7 as above
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.";