0

The future time is :2012-05-26 00:00:00

supposed there are three variable: $hour $minute $second

now, i want to using the future time subtract now time. then give the left hour to $hour,give the left minute to $minute,give the left second to $second.

i am sorry i am new of php, now i get stucked how to do the math operation ? thank you

run
  • 543
  • 4
  • 8
  • 20
  • 1
    Read [the manual](http://www.php.net/manual/en/datetime.diff.php) or [use the search function](http://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php) – PenguinCoder May 22 '12 at 02:13

3 Answers3

2

A very good resource for dates and time..

http://www.php.net/manual/en/function.time.php

-there are samples here doing something similar.

IEnumerable
  • 3,610
  • 14
  • 49
  • 78
1

Check the date_diff function. There's the exact solution to what you're asking there.

And here's the page (DateInterval::format) documenting how you can format the output.

$now = date_create();
// use "now" and necessary DateTimeZone in the arguments
$otherDate = date_create('2020-04-13');
$interval = date_diff($now, $futureDate);
echo $interval->format('%a days');
inhan
  • 7,394
  • 2
  • 24
  • 35
0

The following are the math operations for the difference in hours,minutes and seconds

$future_datetime = '2012-05-26 00:00:00';
$future = strtotime($future_datetime); //future datetime in seconds
$now_datetime = date('Y-m-d H:i:s');
$now = date('U'); //now datetime in seconds

//The math for calculating the difference in hours, minutes and seconds
$difference = $future - $now;
$second = 1;
$minute = 60 * $second;
$hour = 60 * $minute;
$difference_hours = floor($difference/$hour);
$remainder = $difference - ($difference_hours * $hour);
$difference_minutes = floor($remainder/$minute);
$remainder = $remainder - ($difference_minutes * $minute);
$difference_seconds = $remainder;
echo "The difference between $future_datetime and $now_datetime is $difference_hours hours, $difference_minutes minutes and $difference_seconds seconds";
ephemeron
  • 396
  • 2
  • 12