I am trying to find the time difference between 2 timestamps.
Timestamp "startTime" have 1601096400
Timestamp "expireTime" have 1601094600
I want to output the difference between both time like
Expire in : 4hr 30min
How do I do it in php ?
You can use this function
function timestamp_diff($a,$b){
$datetime1 = new DateTime($a);
$datetime2 = new DateTime($b);
$interval = $datetime1->diff($datetime2);
$d=$interval->format('%a');
$h=$interval->format('%h');
$m=$interval->format('%i');
return 'Expire in :'.$d.'day and '.$h.'hr'.$m.'min'
}
Try This is work proper.
<?php
$diff = 1601096400 - 1601094600;
//if use date so try this
$diff = strtotime("2020-09-26 08:10:00") - strtotime("2020-10-26 04:42:00");
$days = floor($diff / 86400);
$hours = floor(($diff - ($days * 86400)) / 3600);
$minutes = floor(($diff - ($days * 86400) - ($hours * 3600))/60);
$seconds = floor(($diff - ($days * 86400) - ($hours * 3600) - ($minutes*60)));
echo 'Expire in : '$hours.'hr'.$minutes.'min';
?>
Related
$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'm trying to get the difference between two date-times and return it as a minute. Date & Time are taken in date("Y-m-d H:i:s") format. But it seem i can't get it right. I did it
$time=date("Y-m-d H:i:s");
$time=date("2014-01-13 08:18:25");
$interval = $time->diff($login_time);
$elapsed = $interval->format(%i minutes);
echo $elapsed;
And This is showing a massage
"Call to a member function diff() on a non-object"
As I am not good enough with date formatting. So Please help me.
What is the way to go about this?
Try this:
$date1 = new DateTime('2013-01-13 04:10:58');
$datediff = $date1->diff(new DateTime('2013-09-11 10:25:00'));
echo $datediff->i;
For more details see this link : http://www.php.net/manual/en/book.datetime.php
Get difference is minutes between two dates:
$now = new DateTime;
$before = new DateTime('2014-01-10 08:18:25');
$diff = $now->diff($before);
$elapsed = $diff->days * 24 * 60 + $diff->h * 60 + $diff->i;
demo
Use the below code for getting time in all expected parameter.
$time1=date("Y-m-d H:i:s");
$time2=date("2014-01-13 08:18:25");
$seconds = strtotime($time1) - strtotime($start);
$year = floor(($seconds)/(60*60*24*365)); $months = floor($seconds /
86400 / 30 ); $week = floor($seconds / 604800); $days =
floor($seconds / 86400); $hours = floor(($seconds - ($days * 86400))
/ 3600); $minutes = floor(($seconds - ($days * 86400) - ($hours *
3600))/60); $seconds = floor(($seconds - ($days * 86400) - ($hours *
3600) - ($minutes*60)));
$time1 = "01:00";
$time2 = "04:55";
list($hours1, $minutes1) = explode(':', $time1);
$startTimestamp = mktime($hours1, $minutes1);
list($hours2, $minutes2) = explode(':', $time2);
$endTimestamp = mktime($hours2, $minutes2);
$seconds = $endTimestamp - $startTimestamp;
$minutes = ($seconds / 60) % 60;
$hours = round($seconds / (60 * 60));
echo $hours.':'.$minutes;
exit;
Outputs 4:55, should be 3:55 ?
Whats wrong here? If it is 01:00 and 02:00, it works fine, but not with the above?
Use floor instead of round...
Or just cast to integer.
$hours = (int) ($seconds / (60 * 60));
Too many calculations when PHP can do it for you with also reducing possibility of error
$time1 = Datetime::createFromFormat("h:i", "01:00");
$time2 = Datetime::createFromFormat("h:i", "04:55");
$diff = $time1->diff($time2);
var_dump($diff->format("%h %i"));
Output
string '3:55' (length=4)
You can also save yourself some time by using strtotime:
$time1 = strtotime("01:00");
$time2 = strtotime("04:55");
$seconds = $time2-$time1;
$minutes = ($seconds / 60) % 60;
$hours = floor($seconds / (60 * 60));
echo $hours.':'.$minutes;
As mentioned, using floor will produce the result you need:
Result
3:55
I am working out the months, days, hours and minutes between two dates, I have successfully got it to work out the months, days and minutes, but I cannot for the life of get it to work out the minutes, below is my code.
<?php
$date1 = "2012-07-01 00:00:00";
$date2 = "2012-09-30 00:00:00";
$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));
$minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24) / (60*60*24) / (60*60));
printf("%d years, %d months, %d days, %d minutes\n", $years, $months, $days, $minutes);
?>
The best and most accurate way to approach this problem is to use the DateTime class. Otherwise you'll run into issues when you deal with anomalies in dates (leap year, etc).
$format = 'Y-m-d h:i:s';
$tz = new DateTimeZone('America/New_York');
// Create DateTime objs based on the above format
$t1 = DateTime::createFromFormat( $format, "2012-07-01 00:00:00", $tz);
$t2 = DateTime::createFromFormat( $format, "2012-09-30 00:00:00", $tz);
// Find the difference between them
$diff = $t1->diff( $t2);
// Print out the difference in each amount
$outputs = array( 'Y' => 'Year', 'm' => 'Month', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'seconds');
foreach( $outputs as $key => $value)
echo $diff->format( '%'.$key) . ' ' . $value . "\n";
This outputs:
00 Year
2 Month
29 day
0 hour
0 minute
0 seconds
Try reducing your $diff variable as you work everything else out:
$years = floor($diff / 31557600); // 31557600 = 365.25 * 86400
$diff -= $years * 31557600;
// I'd skip months, since different months have a different number of days.
// If you *really* want it, calculate the number of months from the days below.
$days = floor($diff / 86400); // 86400 = 24 * 3600
$diff -= $days * 86400;
$hours = floor($diff / 3600); // 3600 = 60 * 60
$diff -= $days * 3600;
$minutes = floor($diff / 60);
$diff -= $minutes * 60;
// $diff now equals the number of seconds left over
Alternatively, look into PHP's Date/Time object:
$date1 = new DateTime("2012-07-01 00:00:00");
$date2 = new DateTime("2012-09-30 00:00:00");
$diff = $date1->diff($date2); // $diff is a DateInterval object
Look at DateInterval->format() to determine how you want to format your output from $diff.
Used to have one of the following functions, to convert seconds into days/weeks/hh:mm:ss time etc.
That should answer your query.
function duration ($sec, $padHours = false) {
if ($sec > 1209600) return "> ". intval(intval($sec) / 604800) . " weeks";
if ($sec > 604800) return "1 week";
if ($sec > 172800) return "> ". intval(intval($sec) / 86400) . " days";
if ($sec > 86400) return "1 day";
$hms = "";
$hours = intval(intval($sec) / 3600);
$hms .= ($padHours)
? str_pad($hours, 2, "0", STR_PAD_LEFT). ":"
: $hours. ":";
$minutes = intval(($sec / 60) % 60);
$hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ":";
$seconds = intval($sec % 60);
$hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
return $hms;
}
Using the PHP code below, I would expect to get '2' as my output. But I get '1'.
Does anyone know why this is?
$returndate = preg_replace('#(\d+)/(\d+)/(\d+)#', '$3-$2-$1', '2011-03-28');
$departdate = preg_replace('#(\d+)/(\d+)/(\d+)#', '$3-$2-$1', '2011-03-26');
$diff = abs(strtotime($returndate) - strtotime($departdate));
$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));
echo $days; // expecting 2, but get 1
Many thanks for any help.
So much calculations... I assume its a rounding issue you have, rounding all the time measurements... here is a simpler look on what you are doing:
function dateDiff($start, $end) {
$start_ts = strtotime($start);
$end_ts = strtotime($end);
$diff = $end_ts - $start_ts;
return round($diff / 86400);
}
$d1 = new DateTime('2011-03-28');
$d2 = new DateTime('2011-03-26');
echo $d1->diff($d2)->d;
Output: 2