Datetime Difference Error - php

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...

Related

PhP month difference between dates in different years

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);

time difference Am I doing it right?

I am trying to measure time difference
I am also learning to wright PHP..
I am saving time and date into SQL as datetime by:
$now = new DateTime();
$datenow = $now->format('Y-m-d H:i:s');
$sql = "INSERT INTO Logg ( logdate , Log , value) VALUES ( '$datenow' , 'Log', '$value' )";
I can now do a query and get:
$result = mysqli_query( $conn , $sqlq );
while($row = mysqli_fetch_array($result)) {
$myLogdate = $row['logdate'];
What I am after, is to be able to check the time between
$myLogdate
and
$now = new DateTime();
$datenow = $now->format('Y-m-d H:i:s');
I have tried:
$interval = $datenow->diff($myLogdate);
$elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds');
echo $elapsed;
But I get no results....
You can use date_diff.
Example from the linked page:
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
A list of possible formats can be found here
for finding the date difference try the below code:
$interval = date_diff($datenow, $myLogdate);
echo $interval->format('%R%a days')
and for format the interval (like "%R%a" means +days) follow http://php.net/manual/en/dateinterval.format.php.
The solution provided by waka was:
$then = new DateTime($myLogDate);
$interval = date_diff($now, $then);
echo $interval->format('%R%a days');

How can I calculate the number of days between 2 variable dates in PHP?

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.

PHP Time String Difference

I need help with my brand new experience with PHP coding!
I have two time strings with the following format:
hh:mm:ss yyyy-mm-dd
find two examples down below:
10:35:56 2013-11-15
11:46:24 2013-11-16
I need to get the difference between the second and the first one (using some PHP function or line of code) in some way that could be reused to compute the same difference of other rows in my DB on demand...
$start=strtotime("10:35:56 2013-11-15");
$end=strtotime("11:46:24 2013-11-16");
$difference=$end-$start;
echo $difference. " Seconds";
use strtotime()
$date1 = strtotime('10:35:56 2013-11-15');
$date2 = strtotime('11:46:24 2013-11-16');
echo $date2 - $date1 . 'seconds';
You can do it this way with strtotime :
$date1 = strtotime("10:35:56 2013-11-15");
$date2 = strtotime("11:46:24 2013-11-16");
$date_diff = $date2 - $date1; // here you got difference in seconds.
echo $date_diff . " seconds";
Or with DateTime object :
$datetime1 = date_create('10:35:56 2013-11-15');
$datetime2 = date_create('11:46:24 2013-11-16');
$date_diff = date_diff($datetime1, $datetime2);
echo $date_diff ->format('%s seconds');
The $date_diff -> format(...) uses this list of format.
For exact difference try this
$datetime1 = new DateTime('10:35:56 2013-11-15');
$datetime2 = new DateTime('11:46:24 2013-11-16');
$interval = $datetime1->diff($datetime2);
echo $interval->m . " Month " .$interval->d ." Days ". $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds <br/>";

How to calculate the difference of datetime field and now in PHP?

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');

Categories