Remaining time beetwen two time - php

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

Related

how to count time from two or more time

$date1 = "01:45:54";
$date2 = "01:16:07";
$diff = abs(strtotime($date2) + strtotime($date1));
$hours = floor(($diff / (60))/ (60));
$minuts = floor(($diff - $hours*60*60)/ 60);
$seconds = floor(($diff - $hours*60*60 - $minuts*60));
printf("%d hours, %d minuts\n, %d seconds\n", $hours, $minuts, $seconds);
result 885685 hours, 2 minuts , 1 seconds (wrong)
i want result 3 hours, 2 minuts , 1 seconds
Thank you in Advance

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

Calculate the difference between two dates in PHP?

I want calculate the difference between two dates in Javascript.
Result should be X years, Y months, Z days
I run below code in PHP but result is different. Which is problem in here?
PHP CODE 1
// Declare and define two dates
$date1 = strtotime("2010-04-01");
$date2 = strtotime("2019-06-30");
// 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);
?>
Result is 9 years, 3 months, 2 days
HOWEVER. if I run below code, Result is 9 years, 2 months, 29days
PHP CODE 2
//差を求める日時の変数を作成
$dateTime1 = '2010-04-01';
$dateTime2 = '2019-06-30';
//DateTimeクラスで2つの日時のオブジェクトを作成
$objDatetime1 = new DateTime($dateTime1);
$objDatetime2 = new DateTime($dateTime2);
//diff()メソッドで2つの日時のオブジェクトから
//ふたつの日付の差をあらわす DateInterval オブジェクトを生成する
$objInterval = $objDatetime1->diff($objDatetime2);
//$objInterval(DateInterval オブジェクト)をformat()メソッドで日時を出力
//%Rは + や - の符号を出力するオプションです
echo $objInterval->format('%R%Y').'year<br/>'; //年
echo $objInterval->format('%R%M').'month<br/>'; //月
echo $objInterval->format('%R%D').'day<br/>'; //日
?>
9 years, 3 months, 2 days and 9 years, 2 months, 29days
what is correct?
A date difference by itself can only be precise to the Day. If you go up to the months and years, then the results will be inevitably false without a date reference.
You can say
9 years, 2 months, 29days till X date
and the reference would be today.
If you take the same period(Year, month, day), and use the date 2019-03-01 as reference, you would have the wrong result.
Here is an example:
$objDatetimeSource1 = new DateTime('2010-04-01');
$objDatetimeSource2 = new DateTime('2019-06-30');
$interval = $objDatetimeSource1->diff($objDatetimeSource2);
$objDatetime1 = new DateTime('2020-02-28');
$objDatetime2 = new DateTime('2020-03-01');
$objDatetime1->add($interval); will give 2029-05-27 00:00:00 and
$objDatetime1->add($interval); will give 2029-05-30 00:00:00 with 3 days difference between them, when, from the dates 2020-02-28 and 2020-03-01, you can clearly see that there is only 2 days difference.

Difference between two time with time period am and pm

I have two times, like this:
$date1 = strtotime("02/12/2019 10:10:54 am");
$date2 = strtotime("02/12/2019 10:11:07 pm");
$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));
$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));
printf(" %d days, %d hours, "
. "%d minutes, %d seconds",
$days, $hours, $minutes, $seconds);
I want to calculate the difference between them, but it's not working.
This is much easier with the DateTime class, which has a diff method which produces a DateInterval object that can output the difference in whatever format you like:
$date1 = new DateTime("02/12/2019 10:10:54 am");
$date2 = new DateTime("02/12/2019 10:11:07 pm");
$diff = $date1->diff($date2);
echo $diff->format('%y years, %m months, %d days, %h hours, %i minutes and %s seconds');
Output:
0 years, 0 months, 0 days, 12 hours, 0 minutes and 13 seconds
Demo on 3v4l.org

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