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'));
Related
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;
Here i am doing task module, now i want to track the time line, here we have two date
Task Due Date (2017-12-28 09:00 PM)
Task Completed On (2017-12-28 09:39 AM)
Using this two dates i want to track their time line,suppose before due date i completed my task means i want to show green color and how many days and how many hours i completed his i want to display, same suppose he is delayed his timeline means i want to show red color and how many days and how many hours i completed his i want to display
i tried but it is not working properly
date_default_timezone_set('Asia/Kolkata');
$due_on = '2017-12-28 09:00 PM';
$start_ex = explode(" ", $due_on);
$start = $start_ex[0].' '.$start_ex[1];
$completedON ='2017-12-28 09:39 AM';
$datetime1 = date_create($start);
$datetime2 = date_create($completedON);
$interval = date_diff($datetime1, $datetime2);
echo $time_diff= $interval->format('%R%a days %h hours');
Updated Code
date_default_timezone_set('Asia/Kolkata');
$compltedOn ='2017-12-28 09:39 PM';//2016-04-17 10:00 AM
$dueDate ='2017-12-28 09:00 PM';//2016-04-17 07:51:30 PM
$datetime1 = date_create($compltedOn);
$datetime2 = date_create($dueDate);
$interval = date_diff($datetime1, $datetime2);
echo $time_diff= $interval->format('%R%a days %h hours %i minuts');//-0 days 0 hours 39 minuts 0 seconds
Two problems:
you are stripping off the PM indicator from the start time so the date_create will treat it as a morning date then compare it with second date - returning difference in minutes.
you are using echo $time_diff= $interval->format('%R%a days %h hours');
so chnage this to $time_diff= $interval->format('%R%a days %h hours');
echo $time_diff;
Amended code:
date_default_timezone_set('Asia/Kolkata');
$compltedOn ='2017-12-28 09:39 PM';//2016-04-17 10:00 AM
$dueDate ='2017-12-28 09:00 PM';//2016-04-17 07:51:30 PM
$datetime1 = date_create($compltedOn);
$datetime2 = date_create($dueDate);
$interval = date_diff($datetime1, $datetime2);
$cls = ($interval->invert == 1 ) ? "red" : "green";
$time_diff= "<span class='{$cls}'>".$interval->format('%R%a days %h hours %i minuts')."<span>";
echo $time_diff;
I am implementing a real Estate application and I need to EXACTLY check the date difference between two dates.
I will prefer for this a javascript jquery approach, but any other solution will be welcome.
My goal is to not allow users to enter dates where the difference is EXACTLY bigger than 2 (or 5) years.
I have tried, along with many others, this solution:
How to get the months between 2 dates
Unfortunately, this is not what I really need.
When I do:
The rent will start at 01-01-2017
And will end at 31-12-2018
I will get 24 months (exactly rented for 2 years), which is correct for my purpose.
But if I try:
The rent will start at 01-01-2017
And will end at 01-01-2019
I will still get 24 months (but it is rented for 2 years and 1 day), which is not correct for my purpose.
Due to the law regulations in my country (Netherlands) there is for the contracts a big difference between the two above mentioned situations.
I will appreciate any help from your side.
As mentioned in the comment, add 2 years and subtract 1 day. Best to do it with DateTime class:
$dt = DateTime::createFromFormat('d-m-Y', '01-01-2017');
$dt->add(new DateInterval('P2Y'));
$dt->sub(new DateInterval('P1D'));
var_dump($dt->format('d-m-Y'));
This will produce the desired result:
string(10) "31-12-2018"
Here's an excellent function by SunilKmCharde, from the User Contributed Notes in the PHP manual:
<?php
//////////////////////////////////////////////////////////////////////
//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);
}
?>
This should be accurate and be aware of leap years etc...
<?php
$date = date_create('02-01-2017');
date_add($date, date_interval_create_from_date_string('2 years'));
$twoyears = date_format($date, 'd-m-Y');
echo $twoyears; // 02-01-2019
$rentperiod = $twoyears->sub(DateInterval::createFromDateString('1 day'));
echo $rentperiod; //01-31-2019
Javascript:
function diff( d1, d2){
x = new Date(d2-d1);
return x.getUTCDate() + " " + x.getUTCMonth() + " " + (x.getUTCFullYear() - 1970);
}
example:
a = new Date('01-01-2017');
b = new Date('01-01-2019');
diff(a,b);
"Days:1, Months: 0, Years: 2"
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)
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?