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 4 years ago.
I used the following code to find out the time past between two dates:
$date1 = "1900-00-00";
$date2 = "2000-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));
printf("%d years, %d months, %d daysn", $years, $months, $days);
But this prints 100 years, 0 months, 24 daysn, instead of 100 years, 0 months, 0 daysn
What is going on?
You can use the DateTime::diff method of the DateTime class to get the difference between two dates. You don't need to calculate the difference yourself:
<?php
$date1 = "1900-00-00";
$date2 = "2000-00-00";
$dt1 = new DateTime($date1);
$dt2 = new DateTime($date2);
$diff = $dt1->diff($dt2);
printf("%d years, %d months, %d days", $diff->y, $diff->m, $diff->d);
//output: 100 years, 0 months, 0 days
You can also use the procedural style to get and output the difference in two lines:
$diff = date_diff(new DateTime("1900-00-00"), new DateTime("2000-00-00"));
printf("%d years, %d months, %d days", $diff->y, $diff->m, $diff->d);
//output: 100 years, 0 months, 0 days
demo: https://ideone.com/rosaJJ
Here is the easiest solution :-
$date1 = "1900-00-00";
$date2 = "2000-00-00";
$expDate = date_create($date2);
$todayDate = date_create($date1);
$diff = date_diff($todayDate, $expDate);
printf("%d years, %d months, %d days", $diff->y, $diff->m, $diff->d);
You will get your expected result .
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);
Here's what I've got so far:
/**
* Parse a duration between 2 date/times in seconds
* and to convert that duration into a formatted string
*
* #param integer $time_start start time in seconds
* #param integer $time_end end time in seconds
* #param string $format like the php strftime formatting uses %y %m %w %d %h or %i.
* #param boolean $chop chop off sections that have 0 values
*/
public static function FormatDateDiff($time_start = 0, $time_end = 0, $format = "%s", $chop = false) {
if($time_start > $time_end) list($time_start, $time_end) = array($time_end, $time_start);
list($year_start,$month_start,$day_start) = explode('-',date('Y-m-d',$time_start));
list($year_end,$month_end,$day_end) = explode('-',date('Y-m-d',$time_end));
$years = $year_end - $year_start;
$months = $month_end - $month_start;
$days = $day_start - $day_end;
$weeks = 0;
$hours = 0;
$mins = 0;
$secs = 0;
if(mktime(0,0,0,$month_end,$day_end) < mktime(0,0,0,$month_start,$day_start)) {
$years -= 1;
}
if($days < 0) {
$months -= 1;
$days += 30; // this is an approximation...not sure how to figure this out
}
if($months < 0) $months += 12;
if(strpos($format, '%y')===false) {
$months += $years * 12;
}
if(strpos($format, '%w')!==false) {
$weeks = floor($days/7);
$days %= 7;
}
echo date('Y-m-d',$time_start).' to '.date('Y-m-d',$time_end).": {$years}y {$months}m {$weeks}w {$days}d<br/>";
}
(It's incomplete and inaccurate)
I can't seem to get the math right. Naively dividing it out won't work because of leap years and differing lengths of months.
The logic also needs to change depending on the format string. For example, passing 04-Feb-2010 to 28-Jun-2011 (as unix timestamps) with format string %y year %m month %d day should output 1 year 4 month 24 day but if %y year is omitted then it needs to add 12 months to the month, i.e., output should be 16 month 24 day.
Should handle times too...but I haven't got to that yet.
None of these date_diff solutions handle weeks. And I don't know how I could hack it into date_diff, so that's not really a solution for me.
Furthermore, $diff->format doesn't do what I asked...to give the total months and days if "bigger units" are omitted. Example:
>>> $start = new DateTime('04-Feb-2010')
>>> $end = new DateTime('28-Jun-2011')
>>> $diff = $start->diff($end)
>>> $diff->format('%m months, %d days')
'4 months, 24 days'
Should be 16 months, 24 days, as I stated earlier. Please stop being so quick to close my question as a dupe before you understand it fully. If the solutions to other questions can be tweaked to solve this, fine, but please explain how, because I don't get it.
To be clear,
if %y is omitted, years should be rolled in the months
if %m is omitted, months should be rolled into the days
if %w is omitted, weeks should be rolled into the days
if %h is omitted, hours should be rolled into minutes
if %m is omitted, minutes should be rolled into seconds
If "smaller units" are omitted, the next biggest unit can be rounded or floored where it makes sense.
I don't expect an accepted answer, but here is how to get date_diff to do weeks.
<?php
$january = new DateTime('2010-01-01');
$february = new DateTime('2011-02-20 3:35:28');
$interval = $february->diff($january);
$parts = $interval->format('%y %m %d %h %i %s %a');
$weeks = 0;
list($years, $months, $days, $hours, $minutes, $seconds, $total_days) = explode(' ', $parts);
if ($days >= 7) {
$weeks = (int)($days / 7);
$days %= 7;
}
echo "$years years, $months months, $weeks weeks, $days days, $hours hours, $minutes minutes $seconds seconds";
// 1 years, 1 months, 2 weeks, 5 days, 3 hours, 35 minutes 28 seconds
Maybe with that you can integrate it into your function to do the rolling over and handling the user given format.
If the bigger units aren't given, you can start from the largest unit and apply them back to the next smaller unit. (i.e. 1 year 1 month with no years should add 12 back to months). If "month" isn't included in the format, then you can use the total days to handle the fact that months have different numbers of days.
I would use DateTime::diff() for this:
$start = new DateTime('2012-01-01 12:00:00');
$end = new DateTime('2012-01-20 06:59:59');
$diff = $start->diff($end);
echo $diff->format('%d days, %h hours, %m minutes, %s seconds');
See DateInterval::format() for more info about the formats.
$absolute = false; //default, returns years, months, days, etc. True would return seconds
$difference = date_diff($dateTimeStart, $dateTimeEnd, $absolute);
echo $difference->format("%y Year(s) %m Month(s) ...");
I would try this first, if it does not work for your purposes then you could try to use the Calendar functions to fetch the correct number of days in each month.
I think you would need to decide upon your own logic for calculating weeks. Personally, I would say $weeks = $difference->d / 7; but there is no strictly correct way to count elapsed weeks in a portion of a month. Think of a calendar, we often speak of first, second, third week, but unless the month started on a Sunday (or Monday, or Saturday, depending on company, religion, etc.) then really we are not being absolute in these descriptions. You can say absolutely that there have been 3 Sundays (for instance) since the beginning of the month, but not three weeks. On the 28th, have four weeks passed? What if the month started on Wed, then is it five?
Also, if you want a custom format (35 Months) you can always access the members directly and concatenate a format string.
Here's my attempt at an interesting problem. It's not quite working 100%, but it's very close.
The function begins by calculating the total number of seconds and months that the date difference requires. Then, using the $tokens array that is sorted in ascending order, it loops through those tokens and tries to match it with the input $format. If the $format is found, the appropriate value is subtracted from the total number of months or seconds.
However, there is a possibility of having a value greater than zero for months or years, but the $format string didn't specify either of those parameters. If so, the function rolls over the appropriate number of days in order to come up with the correct calculations.
I've briefly tested it, and it works for all of the examples in this thread. You can test it out on codepad to see if you can break it! :) I'd be interested in improvements upon this.
<?php
function calc( $start, $end, $format = '%s', $chop = false)
{
$tokens = array( '%y', '%m', '%w', '%d', '%h', '%i', '%s');
if( !is_a( $start, 'DateTime') || !is_a( $end, 'DateTime'))
{
return;
}
$diff = $start->diff( $end);
$months = ($diff->y * 12) + $diff->m;
$secs = ($diff->d * 24 * 3600) +
($diff->h * 3600) +
($diff->i * 60) +
($diff->s);
$output = array();
while( $token = array_shift( $tokens))
{
$token_present = !(strpos( $format, $token) === false);
switch( $token)
{
case '%y':
if( ($months / 12) > 0 && $token_present)
{
$output[$token] = floor( $months / 12);
$months -= $output[$token] * 12;
}
break;
case '%m':
if( $months > 0 && $token_present)
{
$output[$token] = $months;
$months = 0;
}
break;
case '%w':
// Rollover between (months or years) and seconds
if( (!isset( $output['%y']) || !isset( $output['%m'])) && $months > 0)
{
$days = $diff->format( '%a');
// Need a fix for leap year probably.
$days -= (isset( $output['%y'])) ? ($output['%y'] * 365) : 0;
$days -= $diff->d;
$secs += ($days * 24 * 60 * 60);
}
$val = (7 * 24 * 60 * 60);
if( ($secs / $val) > 0 && $token_present)
{
$output[$token] = floor( $secs / $val);
$secs -= $output[$token] * $val;
}
break;
case '%d':
$val = (24 * 60 * 60);
if( ($secs / $val) > 0 && $token_present)
{
$output[$token] = floor( $secs / $val);
$secs -= $output[$token] * $val;
}
break;
case '%h':
$val = (60 * 60);
if( ($secs / $val) > 0 && $token_present)
{
$output[$token] = floor( $secs / $val);
$secs -= $output[$token] * $val;
}
break;
case '%i':
$val = (60);
if( ($secs / $val) > 0 && $token_present)
{
$output[$token] = floor( $secs / $val);
$secs -= $output[$token] * $val;
}
break;
case '%s':
if( $secs > 0 && $token_present)
{
$output[$token] = $secs;
}
break;
}
}
// Filter out blank keys and replace their tokens in the $format string
$filtered = $chop ? array_filter( $output) : $output;
$format = str_replace( array_diff( array_keys($output), array_keys($filtered)), '', $format);
return str_replace( array_keys( $filtered), array_values( $filtered), $format);
}
$start = new DateTime('04-Feb-2010');
$end = new DateTime('28-Jun-2011');
echo calc( $start, $end, "%m months %d days\n"); // 16 months 24 days
$january = new DateTime('2010-01-01');
$february = new DateTime('2011-02-20 3:35:28');
echo calc( $january, $february, '%y years %m months %w weeks %d days %h hours %i minutes %s seconds'); // 1 years 1 months 2 weeks 5 days 3 hours 35 minutes 28 seconds
I think I've got it:
if($time_start > $time_end) list($time_start, $time_end) = array($time_end, $time_start);
$start_dt = new DateTime();
$end_dt = new DateTime();
$start_dt->setTimestamp($time_start);
$end_dt->setTimestamp($time_end);
$has_time = preg_match('`%[his]`',$format) > 0;
if(!$has_time) {
$start_dt->setTime(0,0,0);
$end_dt->setTime(0,0,0);
}
$interval = $end_dt->diff($start_dt);
$parts = $interval->format('%y %m %d %h %i %s %a');
$weeks = 0;
list($years, $months, $days, $hours, $mins, $secs, $total_days) = explode(' ',$parts);
if(strpos($format,'%y')===false) {
$months += $years * 12;
}
if(strpos($format,'%m')===false) {
$days = $total_days;
if(strpos($format,'%y')!==false) {
$start_dt->add(new DateInterval('P'.$years.'Y'));
$interval = $end_dt->diff($start_dt);
$days = $interval->days;
}
}
if(strpos($format,'%w')!==false) {
$weeks = (int)($days/7);
$days %= 7;
}
if(strpos($format,'%d')===false) {
$hours += $days * 24;
}
if(strpos($format,'%h')===false) {
$mins += $hours * 60;
}
if(strpos($format,'%i')===false) {
$secs += $mins * 60;
}
(FYI, I just do some basic str_replacing at the end to throw it into my custom format string)
Edit: There's still 1 more scenario I don't know how to handle... when years and days are requested but not months, the output will be wrong. I'm wondering if I should just mod the total days with 365.... it's kind of a rare scenario.
Edit2: Solved it! We'll just add that many years to the start date and then recalculate the total days.
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