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');
Related
Trying to get the difference between last date vs today.
In a json file, i have unix date:
"lastUpdate": 1568937600,
And i've tried this but with no success.
<?php
$day = $item['lastUpdate'];;
$datetime1 = date_create('$day');
$datetime2 = date_create('now');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a');
?>
Try this:
$datetime1 = new DateTime(date('Y-m-d', $item['lastUpdate'])); //assuming that you have timestamp in the var $item
$datetime2 = new DateTime(date("Y-m-d", strtotime(date("now"))));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a');
Hope it Helps.
Your date_create call on $day is wrong. You need to use double quote to render the variable inside. Also you need an # sign prefix to indicate it to be a timestamp:
<?php
$day = (int) $item['lastUpdate'];
$datetime1 = date_create("#{$day}");
$datetime2 = date_create('now');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a');
?>
Demo: http://sandbox.onlinephpfunctions.com/code/504afecb72bab656bcaf3be8d95bf7f06f5be845
date_create() receives the date/time string in with one of the specific Date and Time Formats. In your case, you're trying to convert a Unix Timestamp to a DateTime object. You can do that properly by replacing the following line:
$datetime1 = date_create('$day');
with:
$datetime1 = date_create('#'.$day);
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');
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 datetime field in my database that contains the following information:
2015-08-04 18:59:01
I want to check the difference between that datetime field and now using Cakephp framework ?
See DateTime::diff
$date = '2015-08-04 18:59:01';
$dateTime = new DateTime($date);
$now = new DateTime();
$interval = $now->diff($dateTime);
echo $interval->format('%R%a days');
See DateInterval::format for other formatting options.
You can also get diff in seconds:
$date = '2015-08-04 18:59:01';
$dateTime = new DateTime($date);
$diff = time() - $dateTime->getTimestamp();
Calculate the difference between two dates:
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
Output:
+272 days
The date_diff() function returns the difference between two DateTime objects.
So I have a question about a DateTime in php.
$datetime1 = new DateTime('2013-02-01 10:40:00');
$datetime2 = new DateTime('2013-02-03 19:13:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%d day %h hours %i minutes');
What echo outputs is: 2day 8hours 33minutes.
Ok I know that the difference between first and the second variable is equal to the output. But is there any way that the output could be some sort of count down.
For example:
$datetime1 = new DateTime('2013-01-01 00:00:00');
$datetime2 = new DateTime('2013-01-01 13:30:00');
What I want to be output is: 13:30:00, and 2 minutes later there would be 13:28:00.
Is there any way to be done that with diff function.
Thanks for help
Sebastian
This will only work if:
one of the times in now
the page refreshes or you use ajax. PHP is executed on the server-side.
So you basically already have the code with just a tweak:
$datetime1 = new DateTime();
$datetime2 = new DateTime('2013-02-03 19:13:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%d day %h hours %i minutes');