Basically i'm getting time difference between current time and database time.
$cur_time = time();
$db_time = $rs[$k]['update_time'];
$diff = abs($cur_time - $db_time);
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
$minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60));
Then to display the time diff in minutes, hours, days etc
if($hours!= 0)
{
if($hours== 1)
{
$time2 = $hours.' hour, ';
}
else
{
$time2 = $hours.' hours, ';
}
}
else
{
$time2 = '';
}
if($minutes!= 0)
{
if($minutes== 1)
{
$time3 = $minuts.' minute, ';
}
else
{
$time3 = $minuts.' minutes, ';
}
}
else
{
$time3 = '';
}
same for seconds, days and months .. Then to display total time difference,
$timediff = $time1.$time2.$time3.$time4.' ago';
If my minutes is greater than 50 , I need to show message else another message.
if($time3 > 50 || $time4 > 1 || $time3 > 1 || $time2 > 1 || $time1 > 1)
{
$msg = 'greater';
}
else
{
$msg = 'lesser';
}
Problem is - Minutes, Hours, days are stored in seperate variable. For suppose, if my time difference is 55 minutes, which is showing
55 minutes ago
my condition is going to if condition which is correct. And if my time difference is 1 hour 2 minutes
1 hour 2 minutes ago
my condition is again going to if condition considering only 2 minutes which is lesser than 50 min, rather it should go to else loop. How to put conditions for these time differences
You can use the DateTime class from PHP:
$dt = new \DateTime('#1434438947'); //unix timestamp
$diff = $dt->diff(new \DateTime(), true);
echo $diff->days * 1440 + $diff->h * 60 + $diff->i; //minutes difference absolute
Why don't you put your condition before conversion?
$cur_time = time();
$db_time = $rs[$k]['update_time'];
$diff = abs($cur_time - $db_time);
if($diff > 3000) // 3600 is 1 hour and 3000 is 50 minutes
{
$msg = 'greater';
}
else
{
$msg = 'lesser';
}
Even if the time difference is 1 hour 2 minutes , this works fine
Related
how to get the time difference between 2 values while if second value is past the current time?
i use below code:
function time_diff($date2, $date1, $format){
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24) / (60*60));
$minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60));
if ($format == 'year'){
$i = $years;
}elseif ($format == 'month'){
$i = $months;
}elseif ($format == 'day'){
$i = $days;
}elseif ($format == 'hour'){
$i = $hours;
}elseif ($format == 'minutes'){
$i = $minutes;
}elseif ($format == 'second'){
$i = $seconds;
}
return $i;
}
i want to get time difference between (saved time in database) and (current time).
everything is fine, but when (saved time in database) passes and its smaller than (current time), this code return value but without negative sign.
i mean if time difference is 2 day, it return 2 while it should return (-2) or somethings like that cuz (saved time in database) passes and its smaller than (current time).
you used $diff = abs(strtotime($date2) - strtotime($date1));, while abs() stands for absolute (as in the result is always positive) using strtotime($date2) - strtotime($date1) should work instead
i have time data in database, i'm recording time with time() function (value 1551866538) on database, and i want write php remaining time for 1 day but as 24 hour. I try date_diff() function but i get error every time, thanks for help.
$mysqltime = $data->registerdate; // 1551866538
$now = time();
$remainingtime = $now - $mysqltime; // I want show as hour
You can also use The DateTime class as below:
$today = new DateTime('now');
$tomorrow = new DateTime(date('Y-m-d H:i:s', '1551866538'));
$difference = $today->diff($tomorrow);
echo $difference->format('%h hours %i minutes remaining');
Output
1 hours 42 minutes remaining
<?php
// Declare and define two dates
$date1 = strtotime("2016-06-01 22:45:00");
$date2 = strtotime("2018-09-21 10:44:01");
// Formulate the Difference between two dates
$diff = abs($date2 - $date1);
// To get the year divide the resultant date into
// total seconds in a year (365*60*60*24)
$years = floor($diff / (365*60*60*24));
// To get the month, subtract it with years and
// divide the resultant date into
// total seconds in a month (30*60*60*24)
$months = floor(($diff - $years * 365*60*60*24)
/ (30*60*60*24));
// To get the day, subtract it with years and
// months and divide the resultant date into
// total seconds in a days (60*60*24)
$days = floor(($diff - $years * 365*60*60*24 -
$months*30*60*60*24)/ (60*60*24));
// To get the hour, subtract it with years,
// months & seconds and divide the resultant
// date into total seconds in a hours (60*60)
$hours = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24)
/ (60*60));
// To get the minutes, subtract it with years,
// months, seconds and hours and divide the
// resultant date into total seconds i.e. 60
$minutes = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24
- $hours*60*60)/ 60);
// To get the minutes, subtract it with years,
// months, seconds, hours and minutes
$seconds = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24
- $hours*60*60 - $minutes*60));
// Print the result
printf("%d years, %d months, %d days, %d hours, "
. "%d minutes, %d seconds", $years, $months,
$days, $hours, $minutes, $seconds);
?>
Output:
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Reference
$mysqltime = $data->registerdate; // 1551866538
$now = time();
$remainingtime = $now - $mysqltime;
echo $hours = round($remainingtime / (60 * 60)); // hours
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 6 years ago.
I need to find the different between 2 timestamp values ,how to calculate it
$time1 ='2016-08-31T07:00:00';
$time2 ='2016-08-31T08:45:00';
I need output time value as 1h 45m
How to get desired output
<?php
$time1 ='2016-08-31T07:00:00';
$time2 ='2016-08-31T08:45:00';
$d = strtotime($time2) - strtotime($time1);
echo gmdate("g", $d),'h ',gmdate('i',$d),'m ';
$date_a = new DateTime('2016-08-31T07:00:00');
$date_b = new DateTime('2016-08-31T08:45:00');
$interval = date_diff($date_a,$date_b);
echo $interval->format('%h Hours:%i Min:%s sec');
You Can test the code Here
Use strtotime() to convert datetime in timestamps and get the difference between 2 times.
Now, Divide the difference into desire format. Use below code.
$time1 ='2016-08-31T07:00:00';
$time2 ='2016-08-31T08:45:00';
$diff = abs(strtotime($time1) - strtotime($time2));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
$minuts = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60));
echo $hours . "h " .$minuts."m";
Output
1h 45m
Live Demo : Click Here
Use this code,
$datetime1 = strtotime("09:00");
$datetime2 = strtotime($items['duration']);
$interval = abs($datetime2 - $datetime1);
$min = round($interval / 60);
$d = floor ($min / 1440);
$h = floor (($min - $d * 1440) / 60);
$m = $min - ($d * 1440) - ($h * 60);
if (!$m) {
$m = '00';
}
echo $h."h:".$m."m";
$date2 = $row['returnbefore'];
$date1 = date('Y/m/d');
$diff = abs(strtotime($date1) - strtotime($date2));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$final = ($days * 10) ;
echo $final;
i want to do a library system when user return book will check the return date between current date and calculate a fine to user which multiply day by 10 .
what step i left ? and i wan set the current time is my computer time so i can test it
This is a fairly simple problem to accomplish using DateTime and DateInterval.
$now = new DateTime();
$dueDate = new DateTime($row['returnbefore']);
$lateInterval = $now - $dueDate;
$daysLate = $lateInterval->format('d');
$fine = $daysLate > 0 ? intval(floor($daysLate)) * 10 : 0; // This means that you are not charged for a late day until the end of the day
I want to subtract one date to another using php and display result in the days - hours - min - sec format. How i do this using php . I tried this using time stamp but it does not give proper values. please give suggestion
For ex : from date 2012-04-27 19:30:56 to date 2012-04-27 19:37:56
i used this code
if(strtotime($history['datetimestamp']) > strtotime($lasttime)) {
$totalelapsed1 = (strtotime($history['datetimestamp'])-strtotime($lasttime));
if($totalelapsed1 > 60 ) {
$sec = $totalelapsed1%60;
$min = round($totalelapsed1/60 , 0);
$minute = $min + $minute;
$second = $sec + $second;
// echo $min. " min " .$sec." sec";
} else {
//echo "0 min " . $totalelapsed1." sec";
$minute = 0 + $minute;
$second = $totalelapsed1 + $second;
}
} else {
$minute = 0 + $minute;
$second = 0 + $second;
// echo "0 min 0 sec";
}
From how to subtract two dates and times to get difference by VolkerK:
You have to use like this:-
<?php
//$now = new DateTime(); // current date/time
$now = new DateTime("2010-07-28 01:11:50");
$ref = new DateTime("2010-07-30 05:56:40");
$diff = $now->diff($ref);
printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);
prints 2 days, 4 hours, 44 minutes
see http://docs.php.net/datetime.diff
edit: But you could also shift the problem more to the database side, e.g. by storing the expiration date/time in the table and then do a query like
... WHERE key='7gedufgweufg' AND expires<Now()
Many rdbms have reasonable/good support for date/time arithmetic.
Link Url:- how to subtract two dates and times to get difference
http://www.webpronews.com/calculating-the-difference-between-two-dates-using-php-2005-11
I suggest to use DateTime and DateInterval objects.
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "days difference ".$interval->d." days ";
read more php DateTime::diff manual
Try using DateTime:diff().
Examples are provided on that page.
This script resolve my issue
$date1 = date("d-m-Y H:i:s",strtotime($date1));
$date2 = date("d-m-Y H:i:s",strtotime($lasttime));
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24)); $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60)); $minuts = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60); $seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60));
printf("%d years, %d months, %d days, %d hours, %d minuts\n, %d seconds\n", $years, $months, $days, $hours, $minuts, $seconds);