Wrong DateInterval using DateTime::diff (- 1 hour) - php

I have this code:
date_default_timezone_set('Europe/Moscow');
$datetime1 = new DateTime('2011-08-07');
$datetime2 = new DateTime('1970-01-01');
$interval = $datetime1->diff($datetime2);
$interval has:
41 years, 7 months, 5 days, 23 hours, 0 minutes, 0 seconds
Why there is 23 hours?

Related

How to correctly calculate time remaining from php date time

The expiry date is in unix timestamp approx 30th sept 2020 6AM
$expiry_date_time = 160142766; //30 sept 2020 6AM
$dt1 = new DateTime();
$dt1->setTimestamp($expiry_date_time);
$now = new DateTime();
$i = $now->diff($dt1);
$time_remaining = $i->format('%y years %m months %a days %h hours %i minutes');
echo $time_remaining;
Output today when i ran the code (11th OCT 2020)
0 years 0 months 11 days 7 hours 19 minutes
This code doesn't give incorrect result but it cannot differentiate between expired and not expired, like in the above case the subscription had expired it gives the output 11 days which is not entirely incorrect because it has been 11 days since its expired if you calculate but shouldn't it be Negative 11 days ?
If I make the expiry date to two days in the future it will correctly calculate and output 2 days, xx hours and xx minutes.
How to make it differentiate between expired and not expired ?
DateInterval has an invert flag for this very purpose:
invert
Is 1 if the interval represents a negative time period and 0 otherwise.
$expiry_date_time = 160142766; //30 sept 2020 6AM
$dt1 = new DateTime();
$dt1->setTimestamp($expiry_date_time);
$now = new DateTime();
// What you're doing right now
$i = $now->diff($dt1);
var_dump($i->invert); // int(1)
// If you're doing it the other way around
$i = $dt1->diff($now);
var_dump($i->invert); // int(0)
Demo
$expiry_date_time = 160142766; //30 sept 2020 6AM
$dt1 = new DateTime();
$dt1->setTimestamp($expiry_date_time);
$now = new DateTime();
$i = $now->diff($dt1);
$time_remaining = $i->format('%y years %m months %a days %h hours %i minutes');
if($expiry_date_time - $now()->getTimestamp() < 0) {
$time_remaining = "expired $time_remaining ago";
}
echo $time_remaining;
This will add some text to the text if it is in the past, to make sure you know if it is expired or not yet.

PHP Display Days, hours and minutes

i am trying to display 'Days' and 'hours' code is working fine, but issue is i don't want to show 0 days. if day= 0 then show only hours.
<?php
$datetime1 = new DateTime();
$datetime2 = new DateTime($feed['dt_date_added']);
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%a days %h hours');
echo $elapsed;
?>
Output : - 0 Days 5 Hour
Expected Output: 5 Hour
You can just check whether the number of days is 0 or not, and adjust your output format accordingly. The DateInterval object provides the "d" property which lets you see the number of days (see documentation).
$datetime1 = new DateTime();
$datetime2 = new DateTime($feed['dt_date_added']);
$interval = $datetime1->diff($datetime2);
$format = "%h hours";
if($interval->d > 0) $format = "%a days ".$format; //adjust the format according to the number of days
$elapsed = $interval->format($format);
echo $elapsed;
Demo: http://sandbox.onlinephpfunctions.com/code/d01ae70566b1b4466664fcff8f7c70b261766c48
You just have to check if the days are 0:
$datetime1 = new DateTime();
$datetime2 = new DateTime($feed['dt_date_added']);
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format( $interval->format('%a') ? '%a days %h hours' : '%h hours' );
echo $elapsed;

Count days between two dates in yii2

Today I am counting days between two dates in yii2. One date is on column of database. Database column is due_date. The other date is current date.
Here is the code for what I done so far in yii2 :
$abc=Yii::$app->db->createCommand('select * from lib_chekout where patron_id=:patron_id AND is_checkedin=0')
->bindValue(':patron_id',$patron_id)
->queryAll();
$datetime1 = $abc[0]['due_date'];
$datetime2 = date("Y-m-d H:i:s");
$interval = $datetime1->diff($datetime2);
print_r($interval);
exit;
My due_date is no $abc array . Now how can I count the dates ?
For reference due_date has date 2011-08-13 00:00:00. And today date is 2016-12-13.
How can I do it ?
as #kashif said you can use date_diff function. first you need to convert you date to DateTimeInterface then pass it to date_diff.
//PARA: Date Should In YYYY-MM-DD Format
//RESULT FORMAT:
// '%y Year %m Month %d Day %h Hours %i Minute %s Seconds' => 1 Year 3 Month 14 Day 11 Hours 49 Minute 36 Seconds
// '%y Year %m Month %d Day' => 1 Year 3 Month 14 Days
// '%m Month %d Day' => 3 Month 14 Day
// '%d Day %h Hours' => 14 Day 11 Hours
// '%d Day' => 14 Days
// '%h Hours %i Minute %s Seconds' => 11 Hours 49 Minute 36 Seconds
// '%i Minute %s Seconds' => 49 Minute 36 Seconds
// '%h Hours => 11 Hours
// '%a Days => 468 Days
//////////////////////////////////////////////////////////////////////
function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
{
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);
$interval = date_diff($datetime1, $datetime2);
return $interval->format($differenceFormat);
}
from here
DateTime::diff -- DateTimeImmutable::diff -- DateTimeInterface::diff -- date_diff — Returns the difference between two DateTime objects
You must convert your date string into datetime object
$abc = Yii::$app->db->createCommand('select * from lib_chekout where patron_id=:patron_id AND is_checkedin=0')
->bindValue(':patron_id',$patron_id)
->queryAll();
$datetime1 = new Datetime($abc[0]['due_date']);
$datetime2 = new Datetime(date("Y-m-d H:i:s"));
$interval = $datetime1->diff($datetime2)->days;
print_r($interval);
exit;
Demo
Try this way.
$datetime1 and $datetime2 should be DateTime object.
$datetime1 = new DateTime('2011-08-13 00:00:00');
//$datetime1 = new DateTime($abc[0]['due_date']);
$datetime2 = new DateTIme('Now');
$interval = $datetime1->diff($datetime2);
print_r($interval->format('%R%a'));

days, hours, and minutes remaining in php

I have this timestamp from the database 1496592689
the problem is I don't know how to get the remaining days, hours and minutes
but I have this code bellow
this is my variable from where the timestamp stored $db_user_timestamp
and now I have this current time now $timenow = time();
I tried to calculate it with
$remainingtime = $db_user_timestamp - $timenow;
But I don't know how to put it in the days, hours and minutes.
Thanks in advance for helping me :)
Always use DateTime
$create_time = "1496592689";
$current_time = time();
$dtCurrent = DateTime::createFromFormat('U', $current_time);
$dtCreate = DateTime::createFromFormat('U', $create_time);
$diff = $dtCurrent->diff($dtCreate);
$interval = $diff->format("%y years %m months %d days %h hours %i minutes %s seconds");
$interval = preg_replace('/(^0| 0) (years|months|days|hours|minutes|seconds)/', '', $interval);
echo $interval;
result
6 months 30 days 5 hours 52 minutes
If your PHP version is 5.3 or latest, you should check
http://php.net/manual/en/class.dateinterval.php
and
http://php.net/manual/en/datetime.diff.php
$datetime1 = new DateTime(date('Y-m-d H:i:s', $db_user_timestamp));
$datetime2 = new DateTime(date('Y-m-d H:i:s'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%y years %m months %d days %h hours %m minutes %s seconds');
<?php
$db_user_timestamp = 1496592689;
$difference = $db_user_timestamp - time();
echo "Day : ".$day = date('d',$difference);
echo "<br>Hour : ".$hour = date('H',$difference);
echo "<br>Minute : ".$minute = date('i',$difference);
?>
Using date('M/d/Y H:i:s', $theTimestamp); will give you date in day, month, year, hour, minute, seconds in the order you want (change 'M/d/Y H:i:s' to your string depending on http://php.net/manual/en/function.date.php)

Time difference is not showing properly

Here is my code : current time - 14.32
$datetime1 = new DateTime('2015-10-12 14:34:00');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->i.' minutes<br>';
The output : 35 Minutes
Why is the minutes is showing 35 instead of 2 minutes?
set the time zone,
date_default_timezone_set("Asia/Kolkata");
$datetime1 = new DateTime('2015-10-12 14:34:00');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->i.' minutes<br>';// for current time, difference is 10 minutes.

Categories