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;
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;
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'));
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 need to calculate the remaining time (days/hours) until a certain date/time.
However, I'm not using a static date.
Imagine I have an event at 17:00 hrs on every Sunday. I need to display the time remaining until the next event, i.e. the oncoming Sunday 17:00.
I've found the following code in this answer. It works for a static date/time, but obviously isn't what I'm looking for.
$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");
Thanks for your time.
You can use the relative time format next Sunday 17:00. Like this:
$now = new DateTime();
$future_date = new DateTime('next Sunday 17:00');
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");
Output:
6 days, 2 hours, 33 minutes, 53 seconds
Read more about relative time formats here: http://www.php.net/manual/en/datetime.formats.relative.php
I'm calculating a timedifference with this bit of php and formatting it in days, hours and minutes.
// Compares expires_at with the current time
$now = new DateTime();
$future_date = new DateTime($contest->expires_at);
$interval = $future_date->diff($now);
$enddate = $interval->format("%a days, %h hours, %i minutes");
// if current time is higher than expiration date set contest to finished.
if($now > $future_date) {
$enddate = 'Date ended';
}
Now I want to have the format to only display the total amount of days when it's over a day(24 hours) and start to format in hours and minutes when it's less than a day(24 hours). So it will format to hours and minutes starting from like 23 hours 59 minutes, you get the idea hopefully.
Can anyone tell me how does it done the easiest?
Do it like this:
// Compares expires_at with the current time
$now = new DateTime();
$future_date = new DateTime($contest->expires_at);
$interval = $future_date->diff($now);
if ($now > $future_date) {
// if current time is higher than expiration date set contest to finished.
$enddate = 'Date ended';
} else if ($interval >= new DateInterval("P1D")) {
$enddate = $interval->format("%a days, %h hours, %i minutes");
} else {
$enddate = $interval->format("%a days, %h hours, %i minutes");
}