0

What would be the best way of converting UK date and time in the format:

30/01/2013 13:30:06

which is d/m/Y H:i:s in PHP noation to relative time (i.e. just now, few minutes ago, 30 minutes ago, 3 hours ago, 1 day ago.. and so on). I've seen several tutorials on the subject but they all revolve around creating functions without any clear explanations. I would appreciate some assistance on the matter.

methuselah
  • 12,766
  • 47
  • 165
  • 315

5 Answers5

4

Hope This helps

function timeSince($ptime){
        $etime = time() - strtotime($ptime);

        if( $etime < 1 ){
            return 'less than 1 second ago';
        }

        $a = array( 12 * 30 * 24 * 60 * 60  =>  'year',
                    30 * 24 * 60 * 60       =>  'month',
                    24 * 60 * 60            =>  'day',
                    60 * 60             =>  'hour',
                    60                  =>  'minute',
                    1                   =>  'second'
        );

        foreach( $a as $secs => $str ){

            $d = $etime / $secs;     
            if( $d >= 1 ){

                $r = round( $d );
                return ' <font style="color:#0099ff"> ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago</font>';
            }
        }
    }
bikash shrestha
  • 429
  • 2
  • 5
2

Use mktime(); and date();

For example if you have a specific time in the format d/m/Y H:i:s, and you want it as d-m-Y H:s:i, you would explode the time into chunks using explode(); and then use date(new_format, mktime(current_format_chunks))

Vishnu R
  • 1,859
  • 3
  • 26
  • 45
phpalix
  • 679
  • 4
  • 8
2

Writing a custom function might help. Cut the string and convert to numbers. Use mktime() to create a timestamp, compare it to time() current timestamp and switch (case) through various relative time possibilities.

Roman Piták
  • 55
  • 2
  • 2
1

dd/mm/yyyy needs to be changed to dd.mm.yyyy according to the formatting rules otherwise it will be treated as mm/dd/yyyy

$dateString = '30/01/2013 13:30:06';
$dateObject = new DateTime(str_replace('/', '.', $dateString));

with the optional addition of a DateTimezone as a second argument to the DateTime constructor.

Then you can do a diff with the current date, and use dateintervals to get the relative time

Vishnu R
  • 1,859
  • 3
  • 26
  • 45
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
1

The most straight-forward and friendly way of parsing dates is the DateTime extension. It has a static method called createFromFormat:

$date   = '30/01/2013 13:30:06';
$format = 'j/m/Y G:i:s';
$time   = DateTime::createFromFormat($format, $date);
echo $time->format('l dS F \'y at H.i.s');

The method takes a custom format and a date string. Because you can define the format yourself it is much easier than parsing it "manually".

In order to adjust the date you can use the add(), sub() and modify() methods:

$time->add(new DateInterval('P3DT5H')); // 3 days and 5 hours
echo $time->format('l dS F \'y at H.i.s');

$time->sub(new DateInterval('P9DT1H')); // 9 days and 1 hours
echo $time->format('l dS F \'y at H.i.s');

$time->modify('-1 year -35 days');
echo $time->format('l dS F \'y at H.i.s');

As you can see the modify() method is slightly easier to use. The two other methods use the DateInterval class and an awkward format. It is not difficult (just read the documentation and do as it says), but using actual words (i.e. "-3 days -7 hours") is easier to understand.

Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52