I have this piece of code which is returning a weird result:
$d1 = new DateTime('2018-12-01');
$d2 = new DateTime('2009-02-03');
$interval = $d2->diff($d1);
echo $interval->format('%m months');
die();
It's returning 9 months, which is wrong. The question is mad simple but I couldn't find an answer yet: What am I doing wrong?
You can use Carbon library it works and return 2 month
$d1 = new \Carbon\Carbon('2018-12-01');
$d2 = new \Carbon\Carbon('2019-02-03');
echo $d2->diffInMonths($d1);
die();
https://carbon.nesbot.com/docs/
it works for me!
$datetime1 = date_create('2018-12-01');
$datetime2 = date_create('2009-02-03');
$interval = date_diff($datetime1, $datetime2);
Related
There a number of questions regarding this, but I couldn't find the exact answer.
$datetime1 = new DateTime('2015-01-15');
$datetime2 = new DateTime(date('Y-m-d'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%m months');
As today is 2015-05-11 this returns 3 months. I would like it to always assume it's the 1st day of the month for $datetime1 so it should actually return 4 months
I supposed I could use str_replace() or some other string function to lop off the day part of $datetime1 but I'm assuming there is a more elegant method?
Thanks
If '2015-01-15' is variable. You could do:
$date = '2015-01-15';
$datetime1 = new DateTime( date('Y-m-01', strtotime($date)) );
Or use a substring function.
$date = '2015-01-15';
$datetime1 = new DateTime( substr($date, 0, 7).'-01' );
Substring would be faster but strtotime() can handle various formats.
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 8 years ago.
I have two time values as follows
$start = '13:00:00';
$end = '21:00:00';
I want calculate the difference between these two time values, I want difference value in format like 00:00:00 (here result would be 08:00:00)
I am using below code to calculate difference
$time = date( "h:i:s", strtotime($end) - strtotime($start));
but it gives me result as 01:30:00
please help if anyone have any idea, i do not want to use DateTime class
In my opinion it is not duplicate, if it is tell me the answer if your
can find in duplicate question
$datetime1 = new DateTime('13:00:00');
$datetime2 = new DateTime('21:00:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%H:%I:%S');
Try with this code :
$time1 = strtotime('13:00:00');
$time2 = strtotime('21:00:00');
$diff = $time2 - $time1;
echo 'Difference: '.date('H:i:s', $diff);
Its easier to do that with the DateTime::diff function.
There is an example an that site.
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
I searched online but couldn't see much help on this.
Is there a ready to use function to do this in PHP that I haven't found?
I want to calculate the number of days between 2 variable dates e.g.:
$date1 = '01/08/2014'; $date2 = '07/08/2014;
I tried the below but $count is null:
<?php
$date1 = '01/01/14';
$date2 = '07/01/14';
$count = $date1->diff($date2);
echo $count;
?>
Better:
$from = DateTime::createFromFormat("d/m/Y",$date1);
$to = DateTime::createFromFormat("d/m/Y",$date2);
$diff = $from->diff($to,true);
$days = $diff->days;
$now = time(); // or your date as well
$your_date = strtotime("2010-01-01");
$datediff = $now - $your_date;
echo floor($datediff/(60*60*24));
Or you can even use PHP's date_diff function like this:
$interval = $date1->diff($date2);
http://php.net/manual/en/datetime.diff.php
Use DateTime::diff (aka date_diff):
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
Or:
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
You can then get the interval as a integer by calling $interval->days.
I have a problem when I use difference of the datetime.
Here is the php code
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->days;
The correct result should be 2. But unfortunately it results 6015. Even when I change the date, its still 6015. Did you guys encounter this problem? I tried to run the script from other computer but its working.
yes sure man for that you need to assign timezone
try this code
i set it for india
$MNTTZ = new DateTimeZone('Asia/Kolkata');
$datetime1 = new DateTime('2009-10-11',$MNTTZ);
$datetime2 = new DateTime('2009-10-13',$MNTTZ);
$interval = $datetime1->diff($datetime2);
echo $interval->days;
Try this,
$start_date = new DateTime("2009-10-11");
$end_date = new DateTime("2009-10-13");
$interval = $start_date->diff($end_date);
echo "Result " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
you use $interval->days replace with $interval->d." days "
you can check my answer https://stackoverflow.com/a/14938421/718224 on date difference for more information.
may this help you.
make sure to set format()
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
see here...
I have a datetime field in my database that contains the following information:
2012-05-03 17:34:01
I want to check the difference between the datetime field and now:
$now = date("Y-m-d H:i:s");
I am attempting to work out how many days have passed between now and the time written to the database field.
How can I achieve this?
Here is the answer :)
$now = new DateTime();
$date = new DateTime("2012-05-03 17:34:01");
echo $date->diff($now)->format("%d days, %h hours and %i minutes");
$diff = abs(strtotime($date2) - strtotime($date1));
date_diff:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime("now");
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');