This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 4 years ago.
how can I get difference between 2 time in hours.
For ex:
$data1 = '2018-04-24 02:30:00';
$date2 = now();
how to get diff between $date1 and $date2.
EDIT: code posted by OP in comments
<?php //date_default_timezone_set('UTC+6');
$time1 = strtotime('2018-04-25 12:00:00');
$time2 = time();
echo $time2.'<br>';
echo date('Y-m-d h:i:s', $time2).'<br>';
echo ($time1-$time2)/3600; ?>
<?php
$datetime1 = new DateTime('2018-04-24 02:30:00');
$datetime2 = new DateTime(date('Y-m-d H:i:s'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%Y %m %d %H:%I:%S');
The result will be:
00 0 1 08:06:15
00 --> years
0 --> months
1 --> days
08 --> hours
06 --> minutes
15 --> seconds
You can modify it as you like but i suggest you keep at least days cause the hours may differ by a few hours but the days can differ by many days.
$data1 = '2018-04-24 02:30:00';
$data2 = date('y-m-d H:i:s');
$formated_in = date('Y-m-d H:i:s', strtotime($data1));
$formated_out = date('Y-m-d H:i:s', strtotime($data2));
$formated_new_in = strtotime($formated_in);
$formated_new_out = strtotime($formated_out);
$sub_total = $formated_new_out - $formated_new_in;
$sub_total = gmdate("H:i", $sub_total);
echo $sub_total;
Related
This question already has answers here:
PHP, need to subtract 12 hours and 30 minutes from a DateTime
(10 answers)
Closed 6 years ago.
how can I subtract some hour and minute form the current datetime in php?
example:
current date: 2016-11-22 14:15:50
I need to deduce 1 hr & 25 minute from this ..
how can I do this?
$time = time()-1*60*60-25*60;
echo date('Y-m-d H:i:s', strtotime($time));
Using simply strtotime
echo date("Y-m-d H:i:s",strtotime("-1 hour -25 minutes"));
Using DateTime class
$date = new DateTime("-1 hour -25 minutes");
echo $date->format("Y-m-d H:i:s");
This code might help you :
$newTime = strtotime('-15 minutes'); // as per your question it should be 85
echo 'Time: '.date('Y-m-d H:i:s', $newTime);
Easy solution: with PHP DateTime
$date = new DateTime('2016-11-22 14:15:50');
$date->sub(new DateInterval('P0Y0M0DT1H25M0S'));
echo $date->format('Y-m-d H:i:s') . "\n";
P is period & T is Time.And you know: Y= Year, M= Month, H= Hour, M= Minutes, and S= Second.Just put your required time before these.You can blank DateTime() to get current time.
WORKING DEMO
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 6 years ago.
I have two values:
$c_time = 2016-03-21 14:56:05
$e_time = 2016-03-24 14:56:05
Now I want to show remaining time and days like this:
3 days 0 hour 0 minute 0 second
How I can do this using PHP?
You can try this
$c_time = new DateTime('2016-03-21 14:56:05');
$e_time = new DateTime('2016-03-24 14:56:05');
$date_diff = $c_time->diff($e_time);
echo "{$date_diff->days} days {$date_diff->h} hour {$date_diff->i} minute {$date_diff->s} second";
- first way:
$datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
$datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');
$secs = $datetime2 - $datetime1;// == return sec in difference
$days = $secs / 86400;
- Another way:
$date1= new DateTime("May 3, 2012 10:38:22 GMT");
$date2= new DateTime("06 Apr 2012 07:22:21 GMT");
echo $date1->diff($date2)->("%d");
$c_time = new DateTime('2016-03-21 14:56:05');
$e_time = new DateTime('2016-03-24 14:56:05');
$interval = $c_time->diff($e_time);
print $interval->format('%a days %h hour %i minutes %s seconds');
print $interval->days; // to obtain the number of days
print $interval->h; // to obtain the number of hours
print $interval->i; // to obtain the number of minutes
print $interval->s; // to obtain the number of seconds
You can use date_diff():
$c_time = "2016-03-21 15:56:05";
$e_time = "2016-03-24 15:56:05";
$start = date_create($c_time);
$end = date_create($e_time);
$diff=date_diff($end,$start);
//print_r($diff);
echo $diff->d." days, ".$diff->h." hours, ".$diff->m." minutes, ".$diff->s." seconds";
Result:
3 days, 0 hours, 0 minutes, 0 seconds
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 8 years ago.
I have two timestamps let us say
$end_date = 2014-09-09 15:03:10 and now date
date_default_timezone_set('Asia/Calcutta');
$now = date('Y-m-d H:i:s');
I want to calculate number of days remaining .Suppose if that particular date crosses now date and it should display remaining days with -ve value.
I am using the following code
$remaining_days =strtotime($end_date) - strtotime($now) ;
$Result_days = floor($remaining_days /86400);
echo $remaining_days.' '.$Result_days.'<br/>'
Problem is that if the end date = today's date it is displaying -1 . I want to calculate based on time and display remaining days and hours.
Please help me to find out the solution.
Try this:
<?php
$end_date = "2014-10-09 15:03:10";
date_default_timezone_set('Asia/Calcutta');
$now = date('Y-m-d H:i:s');
$diff = strtotime($now) - strtotime($end_date);
$fullDays = floor($diff/(60*60*24));
$fullHours = floor(($diff-($fullDays*60*60*24))/(60*60));
$fullMinutes = floor(($diff-($fullDays*60*60*24)-($fullHours*60*60))/60);
echo "Difference is $fullDays days, $fullHours hours and $fullMinutes minutes.";
Output:
Difference is -30 days, 0 hours and 39 minutes.
Demo:
http://3v4l.org/3auqe
Edit (using DATE OBJECT):
<?php
// Example 1
$end_date = "2014-09-11 20:35:10";
date_default_timezone_set('Asia/Calcutta');
$now = date('Y-m-d H:i:s');
$date1=date_create($now);
$date2=date_create($end_date);
$diff=date_diff($date1,$date2,FALSE);
echo $diff->format("%R%d days, %h hours, %m minutes, %s seconds").PHP_EOL;
//Output:
+2 days, 3 hours, 0 minutes, 44 seconds
// Example 2
$end_date = "2014-09-08 20:35:10";
date_default_timezone_set('Asia/Calcutta');
$now = date('Y-m-d H:i:s');
$date1=date_create($now);
$date2=date_create($end_date);
$diff=date_diff($date1,$date2,FALSE);
echo $diff->format("%R%d days, %h hours, %m minutes, %s seconds").PHP_EOL;
//Output:
-0 days, 20 hours, 0 minutes, 16 seconds
Demo:
http://3v4l.org/dPSgX#vhhvm-320
you can use date_diff php method .you can see example here http://php.net/manual/en/function.date-diff.php
You may try like this:
<?php
$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
$fullHours = floor(($dateDiff-($fullDays*60*60*24))/(60*60));
$fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);
echo "Differernce is $fullDays days, $fullHours hours and $fullMinutes minutes.";
?>
See the Source for more options
$date1 = new DateTime("2014-09-09");
$date2 = new DateTime("2014-09-12");
$interval = $date1->diff($date2);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
The duplicate
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
I have two date's
$date1 = "2014-02-11 04:04:26 AM"
$date2 = "2014-02-11 05:36:56 AM"
I want to calculate the difference and display it as follows
1 hour 32 minutes
Make use of DateTime::diff of the DateTime Class
<?php
$datetime1 = new DateTime('2014-02-11 04:04:26 AM');
$datetime2 = new DateTime('2014-02-11 05:36:56 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%h')." Hours ".$interval->format('%i')." Minutes";
OUTPUT :
1 Hours 32 Minutes
Simply convert both dates to timestamp if dont want to do it in complex way...
Something like this
$dateDiff = intval((strtotime($date1)-strtotime($date2))/60);
$hours = intval($dateDiff/60);
$minutes = $dateDiff%60;
and there you go...
Thank you...
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How many days until XXX date?
Currently I am using this code to determine how many days left till an expected day. But this code shows unexpected result. For example if $last_date is 26 December 2012, then I will get 0 day(s) left. But it should be 1 day(s) left. I think my problem is only with floor() function. Isn't it?
$timezone = "Asia/Dhaka";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
$now = time();
$last_date = strtotime("$year-$month-$day");
$datediff = $last_date - $now;
$day_left=floor($datediff/(60*60*24));
echo "$day_left day(s) left.";
N:B: My timezone is +6 GMT, I mean Asia/Dhaka.
As per the PHP documentation:
<?php
$year = '2012';
$month = '12';
$day = '26';
$current_date = new DateTime(date('Y-m-d'), new DateTimeZone('Asia/Dhaka'));
$end_date = new DateTime("$year-$month-$day", new DateTimeZone('Asia/Dhaka'));
$interval = $current_date->diff($end_date);
echo $interval->format('%a day(s)');
?>