How to find out the days and minutes between 2 timestamps - php

I am trying to find out the years, months, days and minutes between 2 timestamps.
I have used the following code, but it is not working:
$diff = time()-$res['asked_on'];
$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));
I am getting the output as:
0
0
0
$res['asked_on'] = 1331980897 ( It is also a time stamp getting from time() function )

If the $res['asked_on'] is a string of date format, you should turn it to timestamp by using strtotime.
And if your php >= 5.3, you could take a look at the method php provide to you: DateTime::diff

Related

PHP Date Difference with Custom Format

I'm facing some problem while calculating the difference between two dates because of Date Format, please help me to fix this issue.
Date 1 - (Format: d/m/Y)
date_default_timezone_set("Asia/Kolkata");
$date1 = date('d/m/Y');
//Output - 20/05/2020
Date 2 - (Format: d/m/Y)
$date2 - 01/27/2020
My Code -
$diff = abs($date2 - $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));
// Print the result
printf("%d years, %d months, %d days", $years, $months, $days);
You don't have to calculate days, month etc manually. There is already DateTime Class available in PHP which you can leverage.
$date1 = DateTime::createFromFormat('d/m/Y', '20/05/2020'); // Use $date1 = new DateTime('NOW'); For Current Time
$date2 = DateTime::createFromFormat('d/m/Y', '25/05/2020');
$interval = $date1->diff($date2);
printf("%d years, %d months, %d days", $interval->y, $interval->m, $interval->d);
Official PHP Documentation: PHP DateTime Class
First you shoud use date format(Format: Y/m/d) and second use strtotime to convert date to seconds because abs function working with numeric values. try following code :
date_default_timezone_set("Asia/Kolkata");
$date1 = strtotime(date('Y/m/d'));
$date2 = strtotime('2020/05/27');
$diff = abs($date2 - $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));
// Print the result
printf("%d years, %d months, %d days", $years, $months, $days);
Output :
0 years, 0 months, 7 days

Find difference between 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 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";

Calculate between two day and calculate a fine

$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

Difference in UK Dates [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 7 years ago.
I am trying to show the difference in days and months between 2 dates
$date1 = $start;
$date2 = $end;
$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));
printf("%d years, %d months, %d days\n", $years, $months, $days);
However this shows as 0 years, 0 months, 0 days
The dates in $start and $end are in the format DD/MM/YYYY
As you have stated within your comment that The dates in $start and $end are in the format DD/MM/YYYY. So you need to first replace that into d-m-Y using str_replace or DateTime::createFromFormat so try using like as
$date1 = str_replace('/','-',$start);
$date2 = str_replace('/','-',$end);
PHP date conversion to strtotime

how to subtract one date to another using php

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);

Categories