PHP Display Days, hours and minutes - php

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;

Related

date and Time difference with color difference

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;

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)

Date time difference miss to calculate the days to hour

I have a date time difference-
I need to calculate those days also into hour.
$start = '2016-05-26 19:05:00';
$end = '2016-05-29 17:05:00';
$datetime1 = new DateTime($start);
$datetime2 = new DateTime($end);
$interval = $datetime1->diff($datetime2);
echo $interval->format("%h Hours %i Min %s Sec"); //22 Hours 0 Min 0 Sec
I know this:
echo $interval->format('%d days');
I try this:
echo $interval->format("(%h + %d * 24) Hours %i Min %s Sec"); //(22 + 8 * 24) Hours 0 Min 0 Sec
$start_date = new DateTime('2007-09-01 04:10:58');
$since_start = $start_date->diff(new DateTime('2012-09-11 10:25:00'));
echo $since_start->days.' days total<br>';
echo $since_start->y.' years<br>';
echo $since_start->m.' months<br>';
echo $since_start->d.' days<br>';
echo $since_start->h.' hours<br>';
echo $since_start->i.' minutes<br>';
echo $since_start->s.' seconds<br>';
echo (($since_start->days)*24)+$since_start->h.' Total Hours<br>';
Use this claculate the days and then multiple by 24 plus remaining hours
Its simple, all though it cannot be done automatically for you, you have to write code.
Here is all you need:
$start = '2016-05-26 19:05:00';
$end = '2016-05-29 17:05:00';
$datetime1 = new DateTime($start);
$datetime2 = new DateTime($end);
$interval = $datetime1->diff($datetime2);
// Below this line is where the magic takes place:
$hourDiff = ($interval->d*24)+($interval->h); //here we calculate the hours
echo $hourDiff //prints 77 because 2 days*24 hours each + 22 hours = 77
In my example I also take into account the years and months (y and m respectively) but you can delete those if you don't want them.

how to calculate the different between two datetime on seconds?

i want to calculate different between two datetime in seconds and check if the result > 300sec
$d1 = new DateTime("2016-03-25 19:29:21");
$d2 = new DateTime(date('Y-m-d H:i:s'));
please check following code:
$datetime1 = new DateTime();
$datetime2 = new DateTime('2016-03-25 19:29:21');
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %S seconds');
echo $elapsed;
check this
if($d2->format('U')-$d1->format('U')>300){
// greater than 300
}else{
// less than 300
}

Php Date Conversion and date difference

I have two dates like
2016-01-25 07:33:54 and current date 30-01-2016 01.27.00
I want to get difference between these dates with dates in number of hours, number of minutes and number of seconds .
$fromDate = "2016-01-25 07:33:54";
$toDate = "30-01-2016 01.27.00";
$difference = strtotime($toDate)-strtotime($fromDate);
echo "Seconds : ".$difference."<br>";
echo "Minutes : ".($difference/60)."<br>";
echo "Hours : ".($difference/(60*60));
Or
A Easy way
$fromDate = "2016-01-25 07:33:54";
$toDate = "30-01-2016 01.27.00";
$datetime1 = new DateTime($toDate);
$datetime2 = new DateTime($fromDate);
$interval = $datetime1->diff($datetime2);
$days = $interval->d;
$hours = $interval->h;
$minutes = $interval->i;
$seconds = $interval->s;
echo "$days days, $hours Hrs, $minutes Mins, $seconds Sec";
http://php.net/manual/en/function.strtotime.php
use strtotime to find time difference between two dates.
http://php.net/manual/en/class.datetime.php
You can also try this.
checkout my code below.
<?php
$datetime1 = new DateTime('2014-02-16 04:04:26 AM');
$datetime2 = new DateTime('2014-02-11 05:36:56 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days')." days ".$interval->format('%h')." Hours ".$interval->format('%i')." Minutes".$interval->format('%s')." Seconds";
?>

Categories