Question

I'm using the Highcharts library to create some nice looking reports.

I have a collection of unix time values and I need to convert them to the amount of hours between that date and "Now".

series: [{
    name: 'Fecha/Hora de Acceso',
    // Outputs: 2382374734 (a unix time)
    data: [<?php foreach($tutores as $t) echo $t['lastlogin'] . ','; ?>]
}]

Is there a simple way to do this using PHP? I'm looking for just a value like 34. Not 34 Hours.

Was it helpful?

Solution

use the PHP time() function to compare and divide the result by 3600 to get the difference in hours

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

<?php foreach($tutores as $t) {
    echo floor((time() - $t['lastlogin']) / 3600) . ',';
} ?>

EDIT: Updated to use floor() in example. Could also use round() or anything else to format the result as you want

OTHER TIPS

This should do it:

$datetime1 = new DateTime();
$datetime2 = new DateTime('@'.$t['lastlogin']);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%h');

Yes, you could DateTime::diff(): http://php.net/datetime.diff

$input = 2382374734;

$old = new DateTime( '@' . $input );
$diff = $old->diff( new DateTime() );

echo sprintf( '%d is %d hours ago', $input, $diff->format( '%h' ) );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top