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');
Related
i want to calculate different between two datetime in seconds and check if the result > 300sec
$d1 = new DateTime("2016-03-25 19:29:21");
$d2 = new DateTime(date('Y-m-d H:i:s'));
please check following code:
$datetime1 = new DateTime();
$datetime2 = new DateTime('2016-03-25 19:29:21');
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %S seconds');
echo $elapsed;
check this
if($d2->format('U')-$d1->format('U')>300){
// greater than 300
}else{
// less than 300
}
I need to be able to find out the difference between two unix epoch times.
I am trying this at the moment
$interval = $nextFile-$firstFile;
($nextFile would equal "1452182820", $firstFile would equal "1452004380")
This gets me a result of "178440".
Is taking away two epoch date times away from each other valid? Or should i find the difference another way.
Try This May be help ful
<?php
$nextFile = '1452182820';
$firstFile = '1452004380';
$n = date('d-m-Y H:i:s',$nextFile);
$f = date('d-m-Y H:i:s',$firstFile);
$Date1 = date("Y-m-d", strtotime($n));
$Date2 = date("Y-m-d", strtotime($f));
$datetime1 = new DateTime($Date1);
$datetime2 = new DateTime($Date2);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a 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 want to get the time difference between 2 dates in minutes. The 2 dates are in the following format
date1='05-11-2012 11:25:00'
date2='06-11-2012 17:45:00'
$datetime1 = new DateTime('05-11-2012 11:25:00');
$datetime2 = new DateTime('06-11-2012 17:45:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days'); //return +1 days
//or
$datetime1 = date_create('05-11-2012 11:25:00');
$datetime2 = date_create('06-11-2012 17:45:00');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days'); //return +1 days
for php <5.3
$date1 = '05-11-2012 11:25:00';
$date2 = '06-11-2012 17:45:00';
$diff = floor(abs( strtotime( $date1 ) - strtotime( $date2 ) )/(60*60*24));
printf("%d days\n", $diff); //return 1 days
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>
For further reference please visit the following link
http://php.net/manual/en/datetime.diff.php
You neglected to indicate what format you'd like the time difference expressed in. If you're willing to work in seconds (which are easily converted), you could simply convert each date to a timestamp and then take the absolute difference using basic math. For instance, give $date1 = '05-11-2012 11:25:00' and $date2 = '06-11-2012 17:45:00':
$diff_seconds = abs( strtotime( $date1 ) - strtotime( $date2 ) );
And you could do whatever you like with the result.
$start_time = strtotime( "2012-10-12 11:35:00" );
$end_time = strtotime( "2012-10-13 12:42:50" );
echo round ( abs( $end_time - $start_time ) / 60,2 ). " minute";
This is different between two dates in MINUTES.
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');