Howto: Calculate the remaining time of the day in PHP

Previously we have talked about calculating the difference between two dates (including time). We have seen how we can use PHP’s built in mktime, date, time functions and PHP 5.3 DateTime class to achieve our goal. Now we shall extend the discussion a little further by calculating the remaining time of the current day. If you are developing some application which depends on the remaining time of the day then this calculation becomes very handy. So, like before we are going to see

  • How to calculate the remaining time of the day using PHP < 5.3 procedural methods.
  • How to calculate the remaining time of the day using PHP >= 5.3 DateTime OOP method.

Before we start, here are:

Online Demo[download id=”16″ format=”1″]

#1: Understanding the concept:

The calculation basically depends on our previous discussion. We basically calculate the difference between

  • The current date and time.
  • The date of tomorrow with hour, minute and second set to zero.
  • Calculate the difference between two and get the second.
  • We’re done.

Sounds pretty simple? It is of course. We shall se how we can do this both using the procedural style as well as OOP style.

#2: Procedural style:

First we get the current date and time:

//get current time
$now = time();

Then we get the tomorrow’s date and time with 0 hours, 0 minutes and 0 seconds.

//get tomorrow's time
$tomorrow = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y'));

Now we subtract to get the number of seconds between them.

//get the remaining time in second
$rem =  $tomorrow - $now;

Remember the itg_sec_to_h function we have used before? We shall use that and we will make the seconds human readable in forms of Hours, Minutes and Seconds.

//make it human readable for PHP < 5.3
$h_rem = itg_sec_to_h($rem);

So, we put together the code and the result is:

/**
 * Calculate the remaining time of the day in procedural style
 */

//get current time
$now = time();
//get tomorrow's time
$tomorrow = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y'));

//get the remaining time in second
$rem =  $tomorrow - $now;

//dump the variable
var_dump($rem);

//make it human readable for PHP < 5.3
$h_rem = itg_sec_to_h($rem);

//dump the variable
var_dump($h_rem);

If we run this the output should be something similar to this:

int 15639

array
  'year' => float 0
  'month' => float 0
  'day' => float 0
  'hour' => float 4
  'minute' => float 20
  'second' => float 39

#3: PHP 5.3 OOP Style:

As mentioned before, PHP 5.3 comes with a class DateTime which handles all the date time operations really good. Let us see how we can use the class to achieve our goal.

First, we create the DateTime object for current date-time:

//current DateTime object
$rd_one = new DateTime('now');

Now we create another object for tomorrow date-time at midnight:

//tomorrow's DateTime object
$rd_two = new DateTime('tomorrow');

We use the diff method to get a DateInterval object containing the difference.

//calculate the difference using DateInterval object
$rd_diff = $rd_one->diff($rd_two);

So, we put together the code and result is:

/**
 * calculate the remaining time in PHP 5.3 OOP method
 */

//current DateTime object
$rd_one = new DateTime('now');
//tomorrow's DateTime object
$rd_two = new DateTime('tomorrow');

//calculate the difference using DateInterval object
$rd_diff = $rd_one->diff($rd_two);

//dump the variable
var_dump($rd_diff);

//dump the format string
var_dump($rd_diff->format('%H hour %i minute %s second %d day %m month %Y year'));

If we run this code, then the output will be:

object(DateInterval)[3]
  public 'y' => int 0
  public 'm' => int 0
  public 'd' => int 0
  public 'h' => int 4
  public 'i' => int 20
  public 's' => int 39
  public 'invert' => int 0
  public 'days' => int 0

string '04 hour 20 minute 39 second 0 day 0 month 00 year' (length=49)

Note how the DateInterval object variables are populated. You don’t even need to convert it to human readable form as it is already. Simply access $rd_diff->h, $rd_diff->i, $rd_diff->s member variables to get the hour, minute and second respectively.

Also, we have used relative date time formatting while creating the basic DateTime objects. You can know more about it here.

Online Demo[download id=”16″ format=”1″]

So that was all. Hope this post was useful for you. Please leave a comment, and if you have any trouble or want to tell us a better method, then simply drop in.