Difference between two timestamp variable in php [duplicate] - php

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

Related

Date time difference in php [duplicate]

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;

Sum date and time and find date difference

I have two variables (first is of type date and second is of type time):
$ref_date = '2016-12-22';
$ref_time = '09:00';
I need to sum these two and find the date difference from now - summed_variable.
Any suggestions?
Using the PHP DateTime object you can do this quite easily
$ref_date = '2016-12-22';
$ref_time = '09:00';
$ref = new DateTime($ref_date . ' ' . $ref_time);
$now = new DateTime();
$diff = $now->diff($ref);
echo $diff->format('%R%a days %H Hours %i Minutes');
And the result would be
+14 days 22 Hours 30 Minutes

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)

How to show difference between two dates and time in php? [duplicate]

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

Calculate time difference between 2 date()'s

I've got an input field where I type in a deadline in words. For example, "5 minutes", "2 days", "6 weeks", "8 months". What I want for the program to do is to calculate how long it will take when that deadline ends. And also if that deadline is almost ending, for example if 80% of the given time has passed.
I was thinking something like that php splits the given time in seconds, and then checks how many minutes and hours or days fit in those seconds and then puts that in dateTime. Like current date + input = futureDate.
I know I probably shouldn't use percentages, it's just an example.
<input type="text" name="getFutureTime">
<?php
$futureTime = $_POST['getFutureTime'];
$dateNow = date('d-m-Y H:i:s');
if($futureTime > $dateNow){
//Calculate
echo "Deadline has passed";
}else if (($futureTime / 100 * 80) < $dateNow){
//Calculate
echo "Deadline is almost passed";
}
?>
Here the values are string and you cant compare this like that. Convert them to timestamp first. Try with -
$futureTime = strtotime($_POST['getFutureTime']);
$dateNow = time();
I have a solution with, caculate how date later :)..., copy this to localhost and run :) diff
$d1=new DateTime("now");
$d2=new DateTime($_POST['DateInput']);
$diff = $d1->diff($d2);
echo "<pre>";
print_r($diff);
echo "</pre>";
$date = $diff->format('%d'); //It date
$year = $diff->format('%y'); //It year
$month = $diff->format('%m'); //It month
echo $diff->format('%a'). "<br/>";
foreach($diff as $key => $value){
echo $key . "<br/>";
echo $value;
}
$value = strtotime('14/12/2012');
echo($value);
Update
$diff->format('%a') // Is date from now example 10/12/2015 - 10/5/2015 = 7
$diff->format('%a') is 7
Try this it will work:
$date_a = new DateTime('2015-05-07 13:03:48');
$date_b = new DateTime('2015-02-04 13:03:41');
$interval = date_diff($date_a,$date_b);
echo $interval->format('%m Months %d Days %h Hours %i Minutes %s Seconds');

Categories