$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
Related
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';
?>
Hey I am trying to get total months from two dates in php. I have searched about leap year calculations between two dates everywhere on internet but did not find the answer.
If my input is "2019-01-01" to "2019-03-31" then the result i expected is 3 months but result i get is 2 month.
Following is my code .
$date1 = strtotime("2019-01-01");
$date2 = strtotime("2019-02-28");
$diff = abs($date2 - $date1);
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24)
/ (30*60*60*24));
printf("%d months",$months);
where am i going wrong
I Understand Your Problem..
Actually the result you are getting is correct,because day ends at night 12pm and you are checking at day time.So until day ends you cant get complete month.If You add one day and check you will get correctly 3 Months
Below I Made Some Changes to Your Code..
$new_date = date('Y-m-d', strtotime('2019-02-28' . ' +1 day'));
$date1 = strtotime("2019-01-01");
$date2 = strtotime($new_date);
$diff = abs($date2 - $date1);
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24)
/ (30*60*60*24));
printf("%d months",$months);
You can Also Get it by Mysql querying to server
SELECT TIMESTAMPDIFF(MONTH, '2019-01-01', (SELECT DATE_ADD('2019-02-28', INTERVAL 1 DAY))) as month
You can Try it.Hope it Helps.
Your math calculates the number of months between the two dates, exclusive of the ending month.
Add 1 to the total, like so.
$months = (floor(($diff - $years * 365*60*60*24)
/ (30*60*60*24))) +1;
You can try the following:
$date1 = "2019-01-01";
$date2 = "2019-02-28";
$timestamp1 = strtotime($date1);
$timestamp2 = strtotime($date2);
$year1 = date('Y', $timestamp1);
$year2 = date('Y', $timestamp2);
$month1 = date('m', $timestamp1);
$month2 = date('m', $timestamp2);
$diff = (($year2 - $year1) * 12) + ($month2 - $month1);
printf("%d months",$diff);
Try it here: http://sandbox.onlinephpfunctions.com/code/496a08612489977e9e23e8ef3ea07ba991bc5e23
You have to add extra month in your $month variable
$date1 = strtotime("2019-01-01");
$date2 = strtotime("2019-03-31");
$diff = abs($date2 - $date1);
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24)
/ (30*60*60*24)) +1 ;
printf("%d months",$months); //Output : give add one extra
OR
$date1 = date_create('2019-01-01');
$date2 date_create('2019-03-31');
$interval= date_diff($date1, $date1);
echo $interval->format('%m months');
So I have used this method to get the difference between 2 dates.
$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));
Now, lets say that I want to convert the years and months into days. How do I do that?
Using DateTime this is a piece of cake:
$date1 = new DateTime($date1);
$date2 = new DateTime($date2);
$diff = $date1->diff($date2, true);
echo $diff->format('%a') . ' days';
$currentDate = date("d-m-Y");
$date1 = date_create("".$joining_date."");
$date2 = date_create("".$currentDate."");
$diff12 = date_diff($date2, $date1);
$hub_days = $diff12->days;
$months = $diff12->m;
$years = $diff12->y;
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);
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