Get difference between 2 dates with unix - php

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

Related

Calculate difference between to unix epoch date times?

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

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

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.

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.

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

Substract 2 datetimes

I need to substract 2 datetimes, I tried a lot of things but nothing seems to work:
$startime = $historial->getStarttime(); //Datetime
$endtime = $historial->getEndtime(); //Datetime
$mytime = $endtime - $startime;
I also tried with strtotime(), date()...
Any help or clue?
Look at diff() method of DateTime object: http://www.php.net/manual/en/datetime.diff.php
<?php
$interval = $startTime->diff($endTime);
It returns a DateInterval method that you can format as you want: http://www.php.net/manual/en/dateinterval.format.php
<?php
echo $interval->format('%d days');
The substraction operator doesn't work like that, you'll need DateTime::diff():
$diff = $starttime->diff($endtime);
This shall return a DateInterval instance, that you can output in whatever format you want.
$datetime1 = new \DateTime('2009-10-11');
$datetime2 = new \DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
$datetime1->sub($interval);
echo $datetime1->format("Y-m-d"); // 2009-10-09

Categories