Header-Ad

PHP,Joomla,WordPress & Linux based server management, error resolutions and fixing problems.

Saturday, October 6, 2012

PHP Get time difference in Hour,Minutes or Seconds.

Starting from PHP 5.3,it has became so much easy for the programmers to code less and get more because of the in-built classes that came with it.One of from those classes is the DateTime() class which made it lot more easier for using it in many different ways,one of the most required technique from this class is getting the difference of time in Years,months,days,hours or even seconds :).

How to get time difference in PHP?

So lets start, we have a scenario that we want to actually get the difference between a past date,suppose a past date you want to compare it with the current date, initiate the class:


$today new DateTime(date('y-m-d h:m:s'));


First we need to get the today's time, Year-Month-Date Hour:Minute:Seconds , so in this way we can totally compare the past time and get the actual difference in every possible way :). Next,


$pastDate $today->diff(new DateTime('2012-09-11 10:25:00'));

Next we are going to compare the past time with the current, so using the DateTime()'s method diff() , since $pastDate is a DateInterval object.

Now you can get the years,months,days,hours or minutes and vice versa, by the following method:


echo $pastDate->y//return the difference in Year(s).
echo $pastDate->m//return the difference in Month(s).
echo $pastDate->d//return the difference in Day(s).
echo $pastDate->h//return the difference in Hour(s).
echo $pastDate->i//return the difference in Minute(s)
echo $pastDate->s//return the difference in Second(s). ​​​

So, now you can actually get the total difference in anyway you want,even in seconds.
Comments are welcome, hope you have learnt these small tricks,and in a way they are allot more helpful!

♥ Happy Coding! ♥



, , ,

4 comments:

  1. The last bit of this isn't exactly true. For instance:

    echo $pastDate->s; //return the difference in Second(s)

    This does not return the difference in seconds, it returns the seconds portion of the difference. Here's an example to illustrate:

    $today = new DateTime(date('2014-04-20 01:07:30'));
    $pastDate = $today->diff(new DateTime('2012-09-11 10:25:00'));

    Then this:

    echo $pastDate->s;

    Will return 30, because this is the difference in the seconds portion of the timestamps. If you want to get the actual number of seconds between the timestamps, you could do something like this:

    echo ($pastDate->days * 86400 + $pastDate->h * 3600 + $pastDate->i * 60 + $pastDate->s);

    ReplyDelete
  2. In fact the first code is wrong
    $today = new DateTime(date('y-m-d h:m:s'));
    this shows minute equals to month, and hour without 24 hour format
    The right code is:
    $today = new DateTime(date('y-m-d H:i:s'));

    ReplyDelete
  3. Can anyone one of you help me out how to calculate diff between 6:00AM to 6:00PM , those two values i got using jquery timepicker.

    Thanks in advance

    ReplyDelete
  4. how to calculate total log in time of employee in a organization using php my sql

    ReplyDelete

** Comments are reviewed, but not delayed posted **