Related
I would like to convert a variable $uptime which is seconds, into days, hours, minutes and seconds.
Example:
$uptime = 1640467;
Result should be:
18 days 23 hours 41 minutes
This can be achieved with DateTime class
Function:
function secondsToTime($seconds) {
$dtF = new \DateTime('#0');
$dtT = new \DateTime("#$seconds");
return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}
Use:
echo secondsToTime(1640467);
# 18 days, 23 hours, 41 minutes and 7 seconds
demo
This is the function rewritten to include days. I also changed the variable names to make the code easier to understand...
/**
* Convert number of seconds into hours, minutes and seconds
* and return an array containing those values
*
* #param integer $inputSeconds Number of seconds to parse
* #return array
*/
function secondsToTime($inputSeconds) {
$secondsInAMinute = 60;
$secondsInAnHour = 60 * $secondsInAMinute;
$secondsInADay = 24 * $secondsInAnHour;
// extract days
$days = floor($inputSeconds / $secondsInADay);
// extract hours
$hourSeconds = $inputSeconds % $secondsInADay;
$hours = floor($hourSeconds / $secondsInAnHour);
// extract minutes
$minuteSeconds = $hourSeconds % $secondsInAnHour;
$minutes = floor($minuteSeconds / $secondsInAMinute);
// extract the remaining seconds
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
$seconds = ceil($remainingSeconds);
// return the final array
$obj = array(
'd' => (int) $days,
'h' => (int) $hours,
'm' => (int) $minutes,
's' => (int) $seconds,
);
return $obj;
}
Source: CodeAid() - http://codeaid.net/php/convert-seconds-to-hours-minutes-and-seconds-(php)
Based on the answer by Julian Moreno, but changed to give the response as a string (not an array), only include the time intervals required and not assume the plural.
The difference between this and the highest voted answer is:
For 259264 seconds, this code would give
3 days, 1 minute, 4 seconds
For 259264 seconds, the highest voted answer (by Glavić) would give
3 days, 0 hours, 1 minutes and 4 seconds
function secondsToTime($inputSeconds) {
$secondsInAMinute = 60;
$secondsInAnHour = 60 * $secondsInAMinute;
$secondsInADay = 24 * $secondsInAnHour;
// Extract days
$days = floor($inputSeconds / $secondsInADay);
// Extract hours
$hourSeconds = $inputSeconds % $secondsInADay;
$hours = floor($hourSeconds / $secondsInAnHour);
// Extract minutes
$minuteSeconds = $hourSeconds % $secondsInAnHour;
$minutes = floor($minuteSeconds / $secondsInAMinute);
// Extract the remaining seconds
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
$seconds = ceil($remainingSeconds);
// Format and return
$timeParts = [];
$sections = [
'day' => (int)$days,
'hour' => (int)$hours,
'minute' => (int)$minutes,
'second' => (int)$seconds,
];
foreach ($sections as $name => $value){
if ($value > 0){
$timeParts[] = $value. ' '.$name.($value == 1 ? '' : 's');
}
}
return implode(', ', $timeParts);
}
I hope this helps someone.
Here it is a simple 8-lines PHP function that converts a number of seconds into a human readable string including number of months for large amounts of seconds:
PHP function seconds2human()
function seconds2human($ss) {
$s = $ss%60;
$m = floor(($ss%3600)/60);
$h = floor(($ss%86400)/3600);
$d = floor(($ss%2592000)/86400);
$M = floor($ss/2592000);
return "$M months, $d days, $h hours, $m minutes, $s seconds";
}
gmdate("d H:i:s",1640467);
Result will be 19 23:41:07. Even if the time is an extra 1 second, it causes the day to change. So it turns out 19. You can explode the result for your needs and fix this.
There are some very good answers here but none of them covered my needs. I built on Glavic's answer to add some extra features that I needed;
Don't print zeros. So "5 minutes" instead of " 0 hours, 5 minutes"
Handle plural properly instead of defaulting to the plural form.
Limit the output to a set number of units; So "2 months, 2 days" instead of "2 months, 2 days, 1 hour, 45 minutes"
You can see a running version of the code here.
function secondsToHumanReadable(int $seconds, int $requiredParts = null)
{
$from = new \DateTime('#0');
$to = new \DateTime("#$seconds");
$interval = $from->diff($to);
$str = '';
$parts = [
'y' => 'year',
'm' => 'month',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
];
$includedParts = 0;
foreach ($parts as $key => $text) {
if ($requiredParts && $includedParts >= $requiredParts) {
break;
}
$currentPart = $interval->{$key};
if (empty($currentPart)) {
continue;
}
if (!empty($str)) {
$str .= ', ';
}
$str .= sprintf('%d %s', $currentPart, $text);
if ($currentPart > 1) {
// handle plural
$str .= 's';
}
$includedParts++;
}
return $str;
}
Short, simple, reliable :
function secondsToDHMS($seconds) {
$s = (int)$seconds;
return sprintf('%d:%02d:%02d:%02d', $s/86400, $s/3600%24, $s/60%60, $s%60);
}
Laravel example
700+ locales support by Carbon
\Carbon\CarbonInterval::seconds(1640467)->cascade()->forHumans(); //2 weeks 4 days 23 hours 41 minutes 7 seconds
The simplest approach would be to create a method that returns a DateInterval from the DateTime::diff of the relative time in $seconds from the current time $now which you can then chain and format. For example:-
public function toDateInterval($seconds) {
return date_create('#' . (($now = time()) + $seconds))->diff(date_create('#' . $now));
}
Now chain your method call to DateInterval::format
echo $this->toDateInterval(1640467)->format('%a days %h hours %i minutes'));
Result:
18 days 23 hours 41 minutes
function convert($seconds){
$string = "";
$days = intval(intval($seconds) / (3600*24));
$hours = (intval($seconds) / 3600) % 24;
$minutes = (intval($seconds) / 60) % 60;
$seconds = (intval($seconds)) % 60;
if($days> 0){
$string .= "$days days ";
}
if($hours > 0){
$string .= "$hours hours ";
}
if($minutes > 0){
$string .= "$minutes minutes ";
}
if ($seconds > 0){
$string .= "$seconds seconds";
}
return $string;
}
echo convert(3744000);
Although it is quite old question - one may find these useful (not written to be fast):
function d_h_m_s__string1($seconds)
{
$ret = '';
$divs = array(86400, 3600, 60, 1);
for ($d = 0; $d < 4; $d++)
{
$q = (int)($seconds / $divs[$d]);
$r = $seconds % $divs[$d];
$ret .= sprintf("%d%s", $q, substr('dhms', $d, 1));
$seconds = $r;
}
return $ret;
}
function d_h_m_s__string2($seconds)
{
if ($seconds == 0) return '0s';
$can_print = false; // to skip 0d, 0d0m ....
$ret = '';
$divs = array(86400, 3600, 60, 1);
for ($d = 0; $d < 4; $d++)
{
$q = (int)($seconds / $divs[$d]);
$r = $seconds % $divs[$d];
if ($q != 0) $can_print = true;
if ($can_print) $ret .= sprintf("%d%s", $q, substr('dhms', $d, 1));
$seconds = $r;
}
return $ret;
}
function d_h_m_s__array($seconds)
{
$ret = array();
$divs = array(86400, 3600, 60, 1);
for ($d = 0; $d < 4; $d++)
{
$q = $seconds / $divs[$d];
$r = $seconds % $divs[$d];
$ret[substr('dhms', $d, 1)] = $q;
$seconds = $r;
}
return $ret;
}
echo d_h_m_s__string1(0*86400+21*3600+57*60+13) . "\n";
echo d_h_m_s__string2(0*86400+21*3600+57*60+13) . "\n";
$ret = d_h_m_s__array(9*86400+21*3600+57*60+13);
printf("%dd%dh%dm%ds\n", $ret['d'], $ret['h'], $ret['m'], $ret['s']);
result:
0d21h57m13s
21h57m13s
9d21h57m13s
function seconds_to_time($seconds){
// extract hours
$hours = floor($seconds / (60 * 60));
// extract minutes
$divisor_for_minutes = $seconds % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
//create string HH:MM:SS
$ret = $hours.":".$minutes.":".$seconds;
return($ret);
}
Solution that should exclude 0 values and set correct singular/plural values
use DateInterval;
use DateTime;
class TimeIntervalFormatter
{
public static function fromSeconds($seconds)
{
$seconds = (int)$seconds;
$dateTime = new DateTime();
$dateTime->sub(new DateInterval("PT{$seconds}S"));
$interval = (new DateTime())->diff($dateTime);
$pieces = explode(' ', $interval->format('%y %m %d %h %i %s'));
$intervals = ['year', 'month', 'day', 'hour', 'minute', 'second'];
$result = [];
foreach ($pieces as $i => $value) {
if (!$value) {
continue;
}
$periodName = $intervals[$i];
if ($value > 1) {
$periodName .= 's';
}
$result[] = "{$value} {$periodName}";
}
return implode(', ', $result);
}
}
I don't know why some of these answers are ridiculously long or complex. Here's one using the DateTime Class. Kind of similar to radzserg's answer. This will only display the units necessary, and negative times will have the 'ago' suffix...
function calctime($seconds = 0) {
$datetime1 = date_create("#0");
$datetime2 = date_create("#$seconds");
$interval = date_diff($datetime1, $datetime2);
if ( $interval->y >= 1 ) $thetime[] = pluralize( $interval->y, 'year' );
if ( $interval->m >= 1 ) $thetime[] = pluralize( $interval->m, 'month' );
if ( $interval->d >= 1 ) $thetime[] = pluralize( $interval->d, 'day' );
if ( $interval->h >= 1 ) $thetime[] = pluralize( $interval->h, 'hour' );
if ( $interval->i >= 1 ) $thetime[] = pluralize( $interval->i, 'minute' );
if ( $interval->s >= 1 ) $thetime[] = pluralize( $interval->s, 'second' );
return isset($thetime) ? implode(' ', $thetime) . ($interval->invert ? ' ago' : '') : NULL;
}
function pluralize($count, $text) {
return $count . ($count == 1 ? " $text" : " ${text}s");
}
// Examples:
// -86400 = 1 day ago
// 12345 = 3 hours 25 minutes 45 seconds
// 987654321 = 31 years 3 months 18 days 4 hours 25 minutes 21 seconds
EDIT: If you want to condense the above example down to use less variables / space (at the expense of legibility), here is an alternate version that does the same thing:
function calctime($seconds = 0) {
$interval = date_diff(date_create("#0"),date_create("#$seconds"));
foreach (array('y'=>'year','m'=>'month','d'=>'day','h'=>'hour','i'=>'minute','s'=>'second') as $format=>$desc) {
if ($interval->$format >= 1) $thetime[] = $interval->$format . ($interval->$format == 1 ? " $desc" : " {$desc}s");
}
return isset($thetime) ? implode(' ', $thetime) . ($interval->invert ? ' ago' : '') : NULL;
}
an extended version of Glavić's excellent solution , having integer validation, solving the 1 s problem, and additional support for years and months, at the expense of being less computer parsing friendly in favor of being more human friendly:
<?php
function secondsToHumanReadable(/*int*/ $seconds)/*: string*/ {
//if you dont need php5 support, just remove the is_int check and make the input argument type int.
if(!\is_int($seconds)){
throw new \InvalidArgumentException('Argument 1 passed to secondsToHumanReadable() must be of the type int, '.\gettype($seconds).' given');
}
$dtF = new \DateTime ( '#0' );
$dtT = new \DateTime ( "#$seconds" );
$ret = '';
if ($seconds === 0) {
// special case
return '0 seconds';
}
$diff = $dtF->diff ( $dtT );
foreach ( array (
'y' => 'year',
'm' => 'month',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second'
) as $time => $timename ) {
if ($diff->$time !== 0) {
$ret .= $diff->$time . ' ' . $timename;
if ($diff->$time !== 1 && $diff->$time !== -1 ) {
$ret .= 's';
}
$ret .= ' ';
}
}
return substr ( $ret, 0, - 1 );
}
var_dump(secondsToHumanReadable(1*60*60*2+1)); -> string(16) "2 hours 1 second"
function secondsToTime($seconds) {
$time = [];
$minutes = $seconds / 60;
$seconds = $seconds % 60;
$hours = $minutes / 60;
$minutes = $minutes % 60;
$days = $hours / 24;
$hours = $hours % 24;
$month = $days /30;
$days = $days % 30;
$year = $month / 12;
$month = $month % 12;
if ((int)($year) != 0){
array_push($time,[ "year" => (int)($year)]);
}
if ($month != 0){
array_push($time, ["months" => $month]);
}
if ($days != 0){
array_push($time,["days" => $days]);
}
if ($hours != 0){
array_push($time,["hours" => $hours]);
}
if ($minutes != 0){
array_push($time,["minutes" => $minutes]);
}
if ($seconds != 0){
array_push($time,["seconds" => $seconds]);
}
return $time;
}
I think Carbon will give you all variety that you want
so for your example you will add this code
$seconds = 1640467;
$time = Carbon::now();
$humanTime = $time->diffForHumans($time->copy()->addSeconds($seconds), true, false, 4);
the output will be like this
2 weeks 4 days 23 hours 41 minutes
Interval class I have written can be used. It can be used in opposite way too.
composer require lubos/cakephp-interval
$Interval = new \Interval\Interval\Interval();
// output 2w 6h
echo $Interval->toHuman((2 * 5 * 8 + 6) * 3600);
// output 36000
echo $Interval->toSeconds('1d 2h');
More info here https://github.com/LubosRemplik/CakePHP-Interval
With DateInterval :
$d1 = new DateTime();
$d2 = new DateTime();
$d2->add(new DateInterval('PT'.$timespan.'S'));
$interval = $d2->diff($d1);
echo $interval->format('%a days, %h hours, %i minutes and %s seconds');
// Or
echo sprintf('%d days, %d hours, %d minutes and %d seconds',
$interval->days,
$interval->h,
$interval->i,
$interval->s
);
// $interval->y => years
// $interval->m => months
// $interval->d => days
// $interval->h => hours
// $interval->i => minutes
// $interval->s => seconds
// $interval->days => total number of days
A bit more elaborated, skipping the time units which are zero
function secondsToTime($ss)
{
$htmlOut="";
$s = $ss%60;
$m = floor(($ss%3600)/60);
$h = floor(($ss%86400)/3600);
$d = floor(($ss%2592000)/86400);
$M = floor($ss/2592000);
if ( $M > 0 )
{
$htmlOut.="$M months";
}
if ( $d > 0 )
{
if ( $M > 0 )
$htmlOut.=", ";
$htmlOut.="$d days";
}
if ( $h > 0 )
{
if ( $d > 0 )
$htmlOut.=", ";
$htmlOut.="$h hours";
}
if ( $m > 0 )
{
if ( $h > 0 )
$htmlOut.=", ";
$htmlOut.="$m minutes";
}
if ( $s > 0 )
{
if ( $m > 0 )
$htmlOut.=" and ";
$htmlOut.="$s seconds";
}
return $htmlOut;
}
All in one solution. Gives no units with zeroes. Will only produce number of units you specify (3 by default).
Quite long, perhaps not very elegant. Defines are optional, but might come in handy in a big project.
define('OneMonth', 2592000);
define('OneWeek', 604800);
define('OneDay', 86400);
define('OneHour', 3600);
define('OneMinute', 60);
function SecondsToTime($seconds, $num_units=3) {
$time_descr = array(
"months" => floor($seconds / OneMonth),
"weeks" => floor(($seconds%OneMonth) / OneWeek),
"days" => floor(($seconds%OneWeek) / OneDay),
"hours" => floor(($seconds%OneDay) / OneHour),
"mins" => floor(($seconds%OneHour) / OneMinute),
"secs" => floor($seconds%OneMinute),
);
$res = "";
$counter = 0;
foreach ($time_descr as $k => $v) {
if ($v) {
$res.=$v." ".$k;
$counter++;
if($counter>=$num_units)
break;
elseif($counter)
$res.=", ";
}
}
return $res;
}
Here's some code that I like to use for the purpose of getting the duration between two dates. It accepts two dates and gives you a nice sentence structured reply.
This is a slightly modified version of the code found here.
<?php
function dateDiff($time1, $time2, $precision = 6, $offset = false) {
// If not numeric then convert texts to unix timestamps
if (!is_int($time1)) {
$time1 = strtotime($time1);
}
if (!is_int($time2)) {
if (!$offset) {
$time2 = strtotime($time2);
}
else {
$time2 = strtotime($time2) - $offset;
}
}
// If time1 is bigger than time2
// Then swap time1 and time2
if ($time1 > $time2) {
$ttime = $time1;
$time1 = $time2;
$time2 = $ttime;
}
// Set up intervals and diffs arrays
$intervals = array(
'year',
'month',
'day',
'hour',
'minute',
'second'
);
$diffs = array();
// Loop thru all intervals
foreach($intervals as $interval) {
// Create temp time from time1 and interval
$ttime = strtotime('+1 ' . $interval, $time1);
// Set initial values
$add = 1;
$looped = 0;
// Loop until temp time is smaller than time2
while ($time2 >= $ttime) {
// Create new temp time from time1 and interval
$add++;
$ttime = strtotime("+" . $add . " " . $interval, $time1);
$looped++;
}
$time1 = strtotime("+" . $looped . " " . $interval, $time1);
$diffs[$interval] = $looped;
}
$count = 0;
$times = array();
// Loop thru all diffs
foreach($diffs as $interval => $value) {
// Break if we have needed precission
if ($count >= $precision) {
break;
}
// Add value and interval
// if value is bigger than 0
if ($value > 0) {
// Add s if value is not 1
if ($value != 1) {
$interval.= "s";
}
// Add value and interval to times array
$times[] = $value . " " . $interval;
$count++;
}
}
if (!empty($times)) {
// Return string with times
return implode(", ", $times);
}
else {
// Return 0 Seconds
}
return '0 Seconds';
}
Source: https://gist.github.com/ozh/8169202
The solution for this one I used (back to the days while learning PHP) without any in-functions:
$days = (int)($uptime/86400); //1day = 86400seconds
$rdays = (uptime-($days*86400));
//seconds remaining after uptime was converted into days
$hours = (int)($rdays/3600);//1hour = 3600seconds,converting remaining seconds into hours
$rhours = ($rdays-($hours*3600));
//seconds remaining after $rdays was converted into hours
$minutes = (int)($rhours/60); // 1minute = 60seconds, converting remaining seconds into minutes
echo "$days:$hours:$minutes";
Though this was an old question, new learners who come across this, may find this answer useful.
a=int(input("Enter your number by seconds "))
d=a//(24*3600) #Days
h=a//(60*60)%24 #hours
m=a//60%60 #minutes
s=a%60 #seconds
print("Days ",d,"hours ",h,"minutes ",m,"seconds ",s)
I am editing one of the code to work it well when negative value comes. floor() function is not giving the correct count when the value is negative. So we need to use abs() function before using it in the floor() function.
$inputSeconds variable can be the difference between the current time stamp and the required date.
/**
* Convert number of seconds into hours, minutes and seconds
* and return an array containing those values
*
* #param integer $inputSeconds Number of seconds to parse
* #return array
*/
function secondsToTime($inputSeconds) {
$secondsInAMinute = 60;
$secondsInAnHour = 60 * $secondsInAMinute;
$secondsInADay = 24 * $secondsInAnHour;
// extract days
$days = abs($inputSeconds / $secondsInADay);
$days = floor($days);
// extract hours
$hourSeconds = $inputSeconds % $secondsInADay;
$hours = abs($hourSeconds / $secondsInAnHour);
$hours = floor($hours);
// extract minutes
$minuteSeconds = $hourSeconds % $secondsInAnHour;
$minutes = abs($minuteSeconds / $secondsInAMinute);
$minutes = floor($minutes);
// extract the remaining seconds
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
$seconds = abs($remainingSeconds);
$seconds = ceil($remainingSeconds);
// return the final array
$obj = array(
'd' => (int) $days,
'h' => (int) $hours,
'm' => (int) $minutes,
's' => (int) $seconds,
);
return $obj;
}
A variation on #Glavić's answer - this one hides leading zeros for shorter results and uses plurals in correct places. It also removes unnecessary precision (e.g. if the time difference is over 2 hours, you probably don't care how many minutes or seconds).
function secondsToTime($seconds)
{
$dtF = new \DateTime('#0');
$dtT = new \DateTime("#$seconds");
$dateInterval = $dtF->diff($dtT);
$days_t = 'day';
$hours_t = 'hour';
$minutes_t = 'minute';
$seconds_t = 'second';
if ((int)$dateInterval->d > 1) {
$days_t = 'days';
}
if ((int)$dateInterval->h > 1) {
$hours_t = 'hours';
}
if ((int)$dateInterval->i > 1) {
$minutes_t = 'minutes';
}
if ((int)$dateInterval->s > 1) {
$seconds_t = 'seconds';
}
if ((int)$dateInterval->d > 0) {
if ((int)$dateInterval->d > 1 || (int)$dateInterval->h === 0) {
return $dateInterval->format("%a $days_t");
} else {
return $dateInterval->format("%a $days_t, %h $hours_t");
}
} else if ((int)$dateInterval->h > 0) {
if ((int)$dateInterval->h > 1 || (int)$dateInterval->i === 0) {
return $dateInterval->format("%h $hours_t");
} else {
return $dateInterval->format("%h $hours_t, %i $minutes_t");
}
} else if ((int)$dateInterval->i > 0) {
if ((int)$dateInterval->i > 1 || (int)$dateInterval->s === 0) {
return $dateInterval->format("%i $minutes_t");
} else {
return $dateInterval->format("%i $minutes_t, %s $seconds_t");
}
} else {
return $dateInterval->format("%s $seconds_t");
}
}
php > echo secondsToTime(60);
1 minute
php > echo secondsToTime(61);
1 minute, 1 second
php > echo secondsToTime(120);
2 minutes
php > echo secondsToTime(121);
2 minutes
php > echo secondsToTime(2000);
33 minutes
php > echo secondsToTime(4000);
1 hour, 6 minutes
php > echo secondsToTime(4001);
1 hour, 6 minutes
php > echo secondsToTime(40001);
11 hours
php > echo secondsToTime(400000);
4 days
Added some formatting modified from Glavić's great answer for Facebook style time of post count up....
function secondsToTime($seconds) {
$dtF = new \DateTime('#0');
$dtT = new \DateTime("#$seconds");
switch($seconds){
case ($seconds<60*60*24): // if time is less than one day
return $dtF->diff($dtT)->format('%h hours, %i minutes, %s seconds');
break;
case ($seconds<60*60*24*31 && $seconds>60*60*24): // if time is between 1 day and 1 month
return $dtF->diff($dtT)->format('%d days, %h hours');
break;
case ($seconds<60*60*24*365 && $seconds>60*60*24*31): // if time between 1 month and 1 year
return $dtF->diff($dtT)->format('%m months, %d days');
break;
case ($seconds>60*60*24*365): // if time is longer than 1 year
return $dtF->diff($dtT)->format('%y years, %m months');
break;
}
foreach ($email as $temp => $value) {
$dat = strtotime($value['subscription_expiration']); //$value come from mysql database
//$email is an array from mysqli_query()
$date = strtotime(date('Y-m-d'));
$_SESSION['expiry'] = (((($dat - $date)/60)/60)/24)." Days Left";
//you will get the difference from current date in days.
}
$value come from Database. This code is in Codeigniter. $SESSION is used for storing user subscriptions. it is mandatory. I used it in my case, you can use whatever you want.
This is a function i used in the past for substracting a date from another one related with your question, my principe was to get how many days, hours minutes and seconds has left until a product has expired :
$expirationDate = strtotime("2015-01-12 20:08:23");
$toDay = strtotime(date('Y-m-d H:i:s'));
$difference = abs($toDay - $expirationDate);
$days = floor($difference / 86400);
$hours = floor(($difference - $days * 86400) / 3600);
$minutes = floor(($difference - $days * 86400 - $hours * 3600) / 60);
$seconds = floor($difference - $days * 86400 - $hours * 3600 - $minutes * 60);
echo "{$days} days {$hours} hours {$minutes} minutes {$seconds} seconds";
I would like to convert a variable $uptime which is seconds, into days, hours, minutes and seconds.
Example:
$uptime = 1640467;
Result should be:
18 days 23 hours 41 minutes
This can be achieved with DateTime class
Function:
function secondsToTime($seconds) {
$dtF = new \DateTime('#0');
$dtT = new \DateTime("#$seconds");
return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}
Use:
echo secondsToTime(1640467);
# 18 days, 23 hours, 41 minutes and 7 seconds
demo
This is the function rewritten to include days. I also changed the variable names to make the code easier to understand...
/**
* Convert number of seconds into hours, minutes and seconds
* and return an array containing those values
*
* #param integer $inputSeconds Number of seconds to parse
* #return array
*/
function secondsToTime($inputSeconds) {
$secondsInAMinute = 60;
$secondsInAnHour = 60 * $secondsInAMinute;
$secondsInADay = 24 * $secondsInAnHour;
// extract days
$days = floor($inputSeconds / $secondsInADay);
// extract hours
$hourSeconds = $inputSeconds % $secondsInADay;
$hours = floor($hourSeconds / $secondsInAnHour);
// extract minutes
$minuteSeconds = $hourSeconds % $secondsInAnHour;
$minutes = floor($minuteSeconds / $secondsInAMinute);
// extract the remaining seconds
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
$seconds = ceil($remainingSeconds);
// return the final array
$obj = array(
'd' => (int) $days,
'h' => (int) $hours,
'm' => (int) $minutes,
's' => (int) $seconds,
);
return $obj;
}
Source: CodeAid() - http://codeaid.net/php/convert-seconds-to-hours-minutes-and-seconds-(php)
Based on the answer by Julian Moreno, but changed to give the response as a string (not an array), only include the time intervals required and not assume the plural.
The difference between this and the highest voted answer is:
For 259264 seconds, this code would give
3 days, 1 minute, 4 seconds
For 259264 seconds, the highest voted answer (by Glavić) would give
3 days, 0 hours, 1 minutes and 4 seconds
function secondsToTime($inputSeconds) {
$secondsInAMinute = 60;
$secondsInAnHour = 60 * $secondsInAMinute;
$secondsInADay = 24 * $secondsInAnHour;
// Extract days
$days = floor($inputSeconds / $secondsInADay);
// Extract hours
$hourSeconds = $inputSeconds % $secondsInADay;
$hours = floor($hourSeconds / $secondsInAnHour);
// Extract minutes
$minuteSeconds = $hourSeconds % $secondsInAnHour;
$minutes = floor($minuteSeconds / $secondsInAMinute);
// Extract the remaining seconds
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
$seconds = ceil($remainingSeconds);
// Format and return
$timeParts = [];
$sections = [
'day' => (int)$days,
'hour' => (int)$hours,
'minute' => (int)$minutes,
'second' => (int)$seconds,
];
foreach ($sections as $name => $value){
if ($value > 0){
$timeParts[] = $value. ' '.$name.($value == 1 ? '' : 's');
}
}
return implode(', ', $timeParts);
}
I hope this helps someone.
Here it is a simple 8-lines PHP function that converts a number of seconds into a human readable string including number of months for large amounts of seconds:
PHP function seconds2human()
function seconds2human($ss) {
$s = $ss%60;
$m = floor(($ss%3600)/60);
$h = floor(($ss%86400)/3600);
$d = floor(($ss%2592000)/86400);
$M = floor($ss/2592000);
return "$M months, $d days, $h hours, $m minutes, $s seconds";
}
gmdate("d H:i:s",1640467);
Result will be 19 23:41:07. Even if the time is an extra 1 second, it causes the day to change. So it turns out 19. You can explode the result for your needs and fix this.
There are some very good answers here but none of them covered my needs. I built on Glavic's answer to add some extra features that I needed;
Don't print zeros. So "5 minutes" instead of " 0 hours, 5 minutes"
Handle plural properly instead of defaulting to the plural form.
Limit the output to a set number of units; So "2 months, 2 days" instead of "2 months, 2 days, 1 hour, 45 minutes"
You can see a running version of the code here.
function secondsToHumanReadable(int $seconds, int $requiredParts = null)
{
$from = new \DateTime('#0');
$to = new \DateTime("#$seconds");
$interval = $from->diff($to);
$str = '';
$parts = [
'y' => 'year',
'm' => 'month',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
];
$includedParts = 0;
foreach ($parts as $key => $text) {
if ($requiredParts && $includedParts >= $requiredParts) {
break;
}
$currentPart = $interval->{$key};
if (empty($currentPart)) {
continue;
}
if (!empty($str)) {
$str .= ', ';
}
$str .= sprintf('%d %s', $currentPart, $text);
if ($currentPart > 1) {
// handle plural
$str .= 's';
}
$includedParts++;
}
return $str;
}
Short, simple, reliable :
function secondsToDHMS($seconds) {
$s = (int)$seconds;
return sprintf('%d:%02d:%02d:%02d', $s/86400, $s/3600%24, $s/60%60, $s%60);
}
Laravel example
700+ locales support by Carbon
\Carbon\CarbonInterval::seconds(1640467)->cascade()->forHumans(); //2 weeks 4 days 23 hours 41 minutes 7 seconds
The simplest approach would be to create a method that returns a DateInterval from the DateTime::diff of the relative time in $seconds from the current time $now which you can then chain and format. For example:-
public function toDateInterval($seconds) {
return date_create('#' . (($now = time()) + $seconds))->diff(date_create('#' . $now));
}
Now chain your method call to DateInterval::format
echo $this->toDateInterval(1640467)->format('%a days %h hours %i minutes'));
Result:
18 days 23 hours 41 minutes
function convert($seconds){
$string = "";
$days = intval(intval($seconds) / (3600*24));
$hours = (intval($seconds) / 3600) % 24;
$minutes = (intval($seconds) / 60) % 60;
$seconds = (intval($seconds)) % 60;
if($days> 0){
$string .= "$days days ";
}
if($hours > 0){
$string .= "$hours hours ";
}
if($minutes > 0){
$string .= "$minutes minutes ";
}
if ($seconds > 0){
$string .= "$seconds seconds";
}
return $string;
}
echo convert(3744000);
Although it is quite old question - one may find these useful (not written to be fast):
function d_h_m_s__string1($seconds)
{
$ret = '';
$divs = array(86400, 3600, 60, 1);
for ($d = 0; $d < 4; $d++)
{
$q = (int)($seconds / $divs[$d]);
$r = $seconds % $divs[$d];
$ret .= sprintf("%d%s", $q, substr('dhms', $d, 1));
$seconds = $r;
}
return $ret;
}
function d_h_m_s__string2($seconds)
{
if ($seconds == 0) return '0s';
$can_print = false; // to skip 0d, 0d0m ....
$ret = '';
$divs = array(86400, 3600, 60, 1);
for ($d = 0; $d < 4; $d++)
{
$q = (int)($seconds / $divs[$d]);
$r = $seconds % $divs[$d];
if ($q != 0) $can_print = true;
if ($can_print) $ret .= sprintf("%d%s", $q, substr('dhms', $d, 1));
$seconds = $r;
}
return $ret;
}
function d_h_m_s__array($seconds)
{
$ret = array();
$divs = array(86400, 3600, 60, 1);
for ($d = 0; $d < 4; $d++)
{
$q = $seconds / $divs[$d];
$r = $seconds % $divs[$d];
$ret[substr('dhms', $d, 1)] = $q;
$seconds = $r;
}
return $ret;
}
echo d_h_m_s__string1(0*86400+21*3600+57*60+13) . "\n";
echo d_h_m_s__string2(0*86400+21*3600+57*60+13) . "\n";
$ret = d_h_m_s__array(9*86400+21*3600+57*60+13);
printf("%dd%dh%dm%ds\n", $ret['d'], $ret['h'], $ret['m'], $ret['s']);
result:
0d21h57m13s
21h57m13s
9d21h57m13s
function seconds_to_time($seconds){
// extract hours
$hours = floor($seconds / (60 * 60));
// extract minutes
$divisor_for_minutes = $seconds % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
//create string HH:MM:SS
$ret = $hours.":".$minutes.":".$seconds;
return($ret);
}
Solution that should exclude 0 values and set correct singular/plural values
use DateInterval;
use DateTime;
class TimeIntervalFormatter
{
public static function fromSeconds($seconds)
{
$seconds = (int)$seconds;
$dateTime = new DateTime();
$dateTime->sub(new DateInterval("PT{$seconds}S"));
$interval = (new DateTime())->diff($dateTime);
$pieces = explode(' ', $interval->format('%y %m %d %h %i %s'));
$intervals = ['year', 'month', 'day', 'hour', 'minute', 'second'];
$result = [];
foreach ($pieces as $i => $value) {
if (!$value) {
continue;
}
$periodName = $intervals[$i];
if ($value > 1) {
$periodName .= 's';
}
$result[] = "{$value} {$periodName}";
}
return implode(', ', $result);
}
}
I don't know why some of these answers are ridiculously long or complex. Here's one using the DateTime Class. Kind of similar to radzserg's answer. This will only display the units necessary, and negative times will have the 'ago' suffix...
function calctime($seconds = 0) {
$datetime1 = date_create("#0");
$datetime2 = date_create("#$seconds");
$interval = date_diff($datetime1, $datetime2);
if ( $interval->y >= 1 ) $thetime[] = pluralize( $interval->y, 'year' );
if ( $interval->m >= 1 ) $thetime[] = pluralize( $interval->m, 'month' );
if ( $interval->d >= 1 ) $thetime[] = pluralize( $interval->d, 'day' );
if ( $interval->h >= 1 ) $thetime[] = pluralize( $interval->h, 'hour' );
if ( $interval->i >= 1 ) $thetime[] = pluralize( $interval->i, 'minute' );
if ( $interval->s >= 1 ) $thetime[] = pluralize( $interval->s, 'second' );
return isset($thetime) ? implode(' ', $thetime) . ($interval->invert ? ' ago' : '') : NULL;
}
function pluralize($count, $text) {
return $count . ($count == 1 ? " $text" : " ${text}s");
}
// Examples:
// -86400 = 1 day ago
// 12345 = 3 hours 25 minutes 45 seconds
// 987654321 = 31 years 3 months 18 days 4 hours 25 minutes 21 seconds
EDIT: If you want to condense the above example down to use less variables / space (at the expense of legibility), here is an alternate version that does the same thing:
function calctime($seconds = 0) {
$interval = date_diff(date_create("#0"),date_create("#$seconds"));
foreach (array('y'=>'year','m'=>'month','d'=>'day','h'=>'hour','i'=>'minute','s'=>'second') as $format=>$desc) {
if ($interval->$format >= 1) $thetime[] = $interval->$format . ($interval->$format == 1 ? " $desc" : " {$desc}s");
}
return isset($thetime) ? implode(' ', $thetime) . ($interval->invert ? ' ago' : '') : NULL;
}
an extended version of Glavić's excellent solution , having integer validation, solving the 1 s problem, and additional support for years and months, at the expense of being less computer parsing friendly in favor of being more human friendly:
<?php
function secondsToHumanReadable(/*int*/ $seconds)/*: string*/ {
//if you dont need php5 support, just remove the is_int check and make the input argument type int.
if(!\is_int($seconds)){
throw new \InvalidArgumentException('Argument 1 passed to secondsToHumanReadable() must be of the type int, '.\gettype($seconds).' given');
}
$dtF = new \DateTime ( '#0' );
$dtT = new \DateTime ( "#$seconds" );
$ret = '';
if ($seconds === 0) {
// special case
return '0 seconds';
}
$diff = $dtF->diff ( $dtT );
foreach ( array (
'y' => 'year',
'm' => 'month',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second'
) as $time => $timename ) {
if ($diff->$time !== 0) {
$ret .= $diff->$time . ' ' . $timename;
if ($diff->$time !== 1 && $diff->$time !== -1 ) {
$ret .= 's';
}
$ret .= ' ';
}
}
return substr ( $ret, 0, - 1 );
}
var_dump(secondsToHumanReadable(1*60*60*2+1)); -> string(16) "2 hours 1 second"
function secondsToTime($seconds) {
$time = [];
$minutes = $seconds / 60;
$seconds = $seconds % 60;
$hours = $minutes / 60;
$minutes = $minutes % 60;
$days = $hours / 24;
$hours = $hours % 24;
$month = $days /30;
$days = $days % 30;
$year = $month / 12;
$month = $month % 12;
if ((int)($year) != 0){
array_push($time,[ "year" => (int)($year)]);
}
if ($month != 0){
array_push($time, ["months" => $month]);
}
if ($days != 0){
array_push($time,["days" => $days]);
}
if ($hours != 0){
array_push($time,["hours" => $hours]);
}
if ($minutes != 0){
array_push($time,["minutes" => $minutes]);
}
if ($seconds != 0){
array_push($time,["seconds" => $seconds]);
}
return $time;
}
I think Carbon will give you all variety that you want
so for your example you will add this code
$seconds = 1640467;
$time = Carbon::now();
$humanTime = $time->diffForHumans($time->copy()->addSeconds($seconds), true, false, 4);
the output will be like this
2 weeks 4 days 23 hours 41 minutes
Interval class I have written can be used. It can be used in opposite way too.
composer require lubos/cakephp-interval
$Interval = new \Interval\Interval\Interval();
// output 2w 6h
echo $Interval->toHuman((2 * 5 * 8 + 6) * 3600);
// output 36000
echo $Interval->toSeconds('1d 2h');
More info here https://github.com/LubosRemplik/CakePHP-Interval
With DateInterval :
$d1 = new DateTime();
$d2 = new DateTime();
$d2->add(new DateInterval('PT'.$timespan.'S'));
$interval = $d2->diff($d1);
echo $interval->format('%a days, %h hours, %i minutes and %s seconds');
// Or
echo sprintf('%d days, %d hours, %d minutes and %d seconds',
$interval->days,
$interval->h,
$interval->i,
$interval->s
);
// $interval->y => years
// $interval->m => months
// $interval->d => days
// $interval->h => hours
// $interval->i => minutes
// $interval->s => seconds
// $interval->days => total number of days
A bit more elaborated, skipping the time units which are zero
function secondsToTime($ss)
{
$htmlOut="";
$s = $ss%60;
$m = floor(($ss%3600)/60);
$h = floor(($ss%86400)/3600);
$d = floor(($ss%2592000)/86400);
$M = floor($ss/2592000);
if ( $M > 0 )
{
$htmlOut.="$M months";
}
if ( $d > 0 )
{
if ( $M > 0 )
$htmlOut.=", ";
$htmlOut.="$d days";
}
if ( $h > 0 )
{
if ( $d > 0 )
$htmlOut.=", ";
$htmlOut.="$h hours";
}
if ( $m > 0 )
{
if ( $h > 0 )
$htmlOut.=", ";
$htmlOut.="$m minutes";
}
if ( $s > 0 )
{
if ( $m > 0 )
$htmlOut.=" and ";
$htmlOut.="$s seconds";
}
return $htmlOut;
}
All in one solution. Gives no units with zeroes. Will only produce number of units you specify (3 by default).
Quite long, perhaps not very elegant. Defines are optional, but might come in handy in a big project.
define('OneMonth', 2592000);
define('OneWeek', 604800);
define('OneDay', 86400);
define('OneHour', 3600);
define('OneMinute', 60);
function SecondsToTime($seconds, $num_units=3) {
$time_descr = array(
"months" => floor($seconds / OneMonth),
"weeks" => floor(($seconds%OneMonth) / OneWeek),
"days" => floor(($seconds%OneWeek) / OneDay),
"hours" => floor(($seconds%OneDay) / OneHour),
"mins" => floor(($seconds%OneHour) / OneMinute),
"secs" => floor($seconds%OneMinute),
);
$res = "";
$counter = 0;
foreach ($time_descr as $k => $v) {
if ($v) {
$res.=$v." ".$k;
$counter++;
if($counter>=$num_units)
break;
elseif($counter)
$res.=", ";
}
}
return $res;
}
Here's some code that I like to use for the purpose of getting the duration between two dates. It accepts two dates and gives you a nice sentence structured reply.
This is a slightly modified version of the code found here.
<?php
function dateDiff($time1, $time2, $precision = 6, $offset = false) {
// If not numeric then convert texts to unix timestamps
if (!is_int($time1)) {
$time1 = strtotime($time1);
}
if (!is_int($time2)) {
if (!$offset) {
$time2 = strtotime($time2);
}
else {
$time2 = strtotime($time2) - $offset;
}
}
// If time1 is bigger than time2
// Then swap time1 and time2
if ($time1 > $time2) {
$ttime = $time1;
$time1 = $time2;
$time2 = $ttime;
}
// Set up intervals and diffs arrays
$intervals = array(
'year',
'month',
'day',
'hour',
'minute',
'second'
);
$diffs = array();
// Loop thru all intervals
foreach($intervals as $interval) {
// Create temp time from time1 and interval
$ttime = strtotime('+1 ' . $interval, $time1);
// Set initial values
$add = 1;
$looped = 0;
// Loop until temp time is smaller than time2
while ($time2 >= $ttime) {
// Create new temp time from time1 and interval
$add++;
$ttime = strtotime("+" . $add . " " . $interval, $time1);
$looped++;
}
$time1 = strtotime("+" . $looped . " " . $interval, $time1);
$diffs[$interval] = $looped;
}
$count = 0;
$times = array();
// Loop thru all diffs
foreach($diffs as $interval => $value) {
// Break if we have needed precission
if ($count >= $precision) {
break;
}
// Add value and interval
// if value is bigger than 0
if ($value > 0) {
// Add s if value is not 1
if ($value != 1) {
$interval.= "s";
}
// Add value and interval to times array
$times[] = $value . " " . $interval;
$count++;
}
}
if (!empty($times)) {
// Return string with times
return implode(", ", $times);
}
else {
// Return 0 Seconds
}
return '0 Seconds';
}
Source: https://gist.github.com/ozh/8169202
The solution for this one I used (back to the days while learning PHP) without any in-functions:
$days = (int)($uptime/86400); //1day = 86400seconds
$rdays = (uptime-($days*86400));
//seconds remaining after uptime was converted into days
$hours = (int)($rdays/3600);//1hour = 3600seconds,converting remaining seconds into hours
$rhours = ($rdays-($hours*3600));
//seconds remaining after $rdays was converted into hours
$minutes = (int)($rhours/60); // 1minute = 60seconds, converting remaining seconds into minutes
echo "$days:$hours:$minutes";
Though this was an old question, new learners who come across this, may find this answer useful.
a=int(input("Enter your number by seconds "))
d=a//(24*3600) #Days
h=a//(60*60)%24 #hours
m=a//60%60 #minutes
s=a%60 #seconds
print("Days ",d,"hours ",h,"minutes ",m,"seconds ",s)
I am editing one of the code to work it well when negative value comes. floor() function is not giving the correct count when the value is negative. So we need to use abs() function before using it in the floor() function.
$inputSeconds variable can be the difference between the current time stamp and the required date.
/**
* Convert number of seconds into hours, minutes and seconds
* and return an array containing those values
*
* #param integer $inputSeconds Number of seconds to parse
* #return array
*/
function secondsToTime($inputSeconds) {
$secondsInAMinute = 60;
$secondsInAnHour = 60 * $secondsInAMinute;
$secondsInADay = 24 * $secondsInAnHour;
// extract days
$days = abs($inputSeconds / $secondsInADay);
$days = floor($days);
// extract hours
$hourSeconds = $inputSeconds % $secondsInADay;
$hours = abs($hourSeconds / $secondsInAnHour);
$hours = floor($hours);
// extract minutes
$minuteSeconds = $hourSeconds % $secondsInAnHour;
$minutes = abs($minuteSeconds / $secondsInAMinute);
$minutes = floor($minutes);
// extract the remaining seconds
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
$seconds = abs($remainingSeconds);
$seconds = ceil($remainingSeconds);
// return the final array
$obj = array(
'd' => (int) $days,
'h' => (int) $hours,
'm' => (int) $minutes,
's' => (int) $seconds,
);
return $obj;
}
A variation on #Glavić's answer - this one hides leading zeros for shorter results and uses plurals in correct places. It also removes unnecessary precision (e.g. if the time difference is over 2 hours, you probably don't care how many minutes or seconds).
function secondsToTime($seconds)
{
$dtF = new \DateTime('#0');
$dtT = new \DateTime("#$seconds");
$dateInterval = $dtF->diff($dtT);
$days_t = 'day';
$hours_t = 'hour';
$minutes_t = 'minute';
$seconds_t = 'second';
if ((int)$dateInterval->d > 1) {
$days_t = 'days';
}
if ((int)$dateInterval->h > 1) {
$hours_t = 'hours';
}
if ((int)$dateInterval->i > 1) {
$minutes_t = 'minutes';
}
if ((int)$dateInterval->s > 1) {
$seconds_t = 'seconds';
}
if ((int)$dateInterval->d > 0) {
if ((int)$dateInterval->d > 1 || (int)$dateInterval->h === 0) {
return $dateInterval->format("%a $days_t");
} else {
return $dateInterval->format("%a $days_t, %h $hours_t");
}
} else if ((int)$dateInterval->h > 0) {
if ((int)$dateInterval->h > 1 || (int)$dateInterval->i === 0) {
return $dateInterval->format("%h $hours_t");
} else {
return $dateInterval->format("%h $hours_t, %i $minutes_t");
}
} else if ((int)$dateInterval->i > 0) {
if ((int)$dateInterval->i > 1 || (int)$dateInterval->s === 0) {
return $dateInterval->format("%i $minutes_t");
} else {
return $dateInterval->format("%i $minutes_t, %s $seconds_t");
}
} else {
return $dateInterval->format("%s $seconds_t");
}
}
php > echo secondsToTime(60);
1 minute
php > echo secondsToTime(61);
1 minute, 1 second
php > echo secondsToTime(120);
2 minutes
php > echo secondsToTime(121);
2 minutes
php > echo secondsToTime(2000);
33 minutes
php > echo secondsToTime(4000);
1 hour, 6 minutes
php > echo secondsToTime(4001);
1 hour, 6 minutes
php > echo secondsToTime(40001);
11 hours
php > echo secondsToTime(400000);
4 days
Added some formatting modified from Glavić's great answer for Facebook style time of post count up....
function secondsToTime($seconds) {
$dtF = new \DateTime('#0');
$dtT = new \DateTime("#$seconds");
switch($seconds){
case ($seconds<60*60*24): // if time is less than one day
return $dtF->diff($dtT)->format('%h hours, %i minutes, %s seconds');
break;
case ($seconds<60*60*24*31 && $seconds>60*60*24): // if time is between 1 day and 1 month
return $dtF->diff($dtT)->format('%d days, %h hours');
break;
case ($seconds<60*60*24*365 && $seconds>60*60*24*31): // if time between 1 month and 1 year
return $dtF->diff($dtT)->format('%m months, %d days');
break;
case ($seconds>60*60*24*365): // if time is longer than 1 year
return $dtF->diff($dtT)->format('%y years, %m months');
break;
}
foreach ($email as $temp => $value) {
$dat = strtotime($value['subscription_expiration']); //$value come from mysql database
//$email is an array from mysqli_query()
$date = strtotime(date('Y-m-d'));
$_SESSION['expiry'] = (((($dat - $date)/60)/60)/24)." Days Left";
//you will get the difference from current date in days.
}
$value come from Database. This code is in Codeigniter. $SESSION is used for storing user subscriptions. it is mandatory. I used it in my case, you can use whatever you want.
This is a function i used in the past for substracting a date from another one related with your question, my principe was to get how many days, hours minutes and seconds has left until a product has expired :
$expirationDate = strtotime("2015-01-12 20:08:23");
$toDay = strtotime(date('Y-m-d H:i:s'));
$difference = abs($toDay - $expirationDate);
$days = floor($difference / 86400);
$hours = floor(($difference - $days * 86400) / 3600);
$minutes = floor(($difference - $days * 86400 - $hours * 3600) / 60);
$seconds = floor($difference - $days * 86400 - $hours * 3600 - $minutes * 60);
echo "{$days} days {$hours} hours {$minutes} minutes {$seconds} seconds";
I want to do what StackOverflow is doing which is saying exactly how long it's been since the last post. There is a catch though, SO displays certain information based on how long the ago the last post was - for example, if the post was less than a day ago, they post how many hours ago the last post was; if the post was less than an hour ago they post how many minutes ago it was, etc.
I'm working with a MYSQL DateTime field in the following format:
2012-09-19 13:28:45
I want to compare the above to the time NOW and so I converted that time using PHP's strtotime function and tried comparing the two dates through a function I put together (below). Granted, this is probably the WORST possible way of doing this but after reading about PHP's Date and DateTime functions I'm starting to become very, very confused.
function get_date_format($strToTimeString){
$minute = 60;
$hour = $minutes * 60;
$day = $hour * 24;
$week = $day * 7;
$month = $week * 4;
$year = $month * 12;
$timeNow = strtotime("now");
$timeDiff = $timeNow - $strToTimeString;
if($timeDiff > $minute){
if($timeDiff > $hour){
if($timeDiff > $day){
if($timeDiff > $week){
if($timeDiff > $month){
if($timeDiff > $year){
// Years ago
}
else{
// Months ago
}
}
else{
// Weeks ago
}
}
else{
// Days ago
}
}
else
{
// Hours ago
}
}
else{
// Minutes ago
}
}
else{
// Seconds ago
}
}
Is there a better way to do this? As I mentioned above, I had no luck when trying to use DateTime->diff
I really appreciate any help.
Use DateTime. Here's sample:
$now = new DateTime('now');
$posted = new DateTime($postDateFromDBHere);
$interval = $posted->diff($now);
var_dump($interval);
echo $interval->format('%y-%m-%d %h:%m:%s'); //You can do similar to format your output as you wish.
Use DateTime and DateTime:diff then check each value:
function returnInterval($date){
$datetime1 = new DateTime($date);
$datetime2 = new DateTime();
$diff = $datetime1->diff($datetime2);
$string = '';
$pass = '';
if($diff->y){
$string .= ($diff->y == 1) ? $diff->y." year" : $diff->y." years";
$pass = ', ';
}
if($diff->m){
$string .= $pass;
$string .= ($diff->m == 1) ? $diff->m." month" : $diff->m." months";
$pass = ', ';
}
if($diff->d){
$string .= $pass;
$string .= ($diff->d == 1) ? $diff->d." day" : $diff->d." days";
$pass = ', ';
}
if($diff->h){
$string .= $pass;
$string .= ($diff->h == 1) ? $diff->h." hour" : $diff->h." hours";
$pass = ', ';
}
if($diff->i){
$string .= $pass;
$string .= ($diff->i == 1) ? $diff->i." minute" : $diff->i." minutes";
$pass = ', ';
}
if($diff->s){
$string .= $pass;
$string .= ($diff->s == 1) ? $diff->s." second" : $diff->s." seconds";
}
$pos = strrpos($string, ',');
$string = substr_replace($string, ' and ', $pos, 2);
return $string;
}
echo returnInterval('2012-09-19 13:28:45');
// 8 days, 13 hours, 47 minutes and 44 seconds
After long searching, I got something close to what rottentomatoes used on their forum:
function realTimeSince($time){
define(MINUTE, 60);
define(HOUR, 60*60);
define(DAY, 60*60*24);
define(MONTH, 60*60*24*30);
$delta = strtotime(gmdate("Y-m-d H:i:s", time())) - strtotime($time);
if ($delta < 1 * MINUTE) {
return $delta == 1 ? "one second ago" : $delta . " seconds ago";
}
if ($delta < 2 * MINUTE) {
return "a minute ago";
}
if ($delta < 45 * MINUTE) {
return floor($delta / MINUTE) . " minutes ago";
}
if ($delta < 90 * MINUTE) {
return "an hour ago";
}
if ($delta < 24 * HOUR) {
return floor($delta / HOUR) . " hours ago";
}
if ($delta < 48 * HOUR) {
return "yesterday";
}
if ($delta < 30 * DAY) {
return floor($delta / DAY) . " days ago";
}
if ($delta < 12 * MONTH) {
$months = floor($delta / DAY / 30);
return $months <= 1 ? "one month ago" : $months . " months ago";
} else {
$years = floor($delta / DAY / 365);
return $years <= 1 ? "one year ago" : $years . " years ago";
}
}
so you can use it like this:
realTimeSince('2012-11-12 00:09:54'); //which can be got from a MySQL TIMESTAMP field
modified a little based on http://www.ferdychristant.com/blog//archive/DOMM-7QEFK4
Try the following function
function time_ago($date)
{
//echo "ss";
if (empty($date)) {
return "No date provided";
}
$periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if (empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if ($now >= $unix_date) {
$difference= $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";
}
for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if ($difference != 1 && $j != 0) {
$periods[$j].= "s";
}
if($difference!=0)
return "$difference $periods[$j] {$tense}";
else
return "a few seconds ago";
}
or
function time_elapsed_since ($postedDateTime){
$time = time() - $postedDateTime; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
time_elapsed_since($postedDateTime).' ago';
I have a php timestamp 1331875634 generated using php time() function.
I have the current timestamp generated using same function.
<?php
$time1 = "1331875634";
$time2 = time();
echo $differencem; //time difference in minutes
echo $differenceh; //time difference in hours
?>
I want to know the difference between these two in minutes. The minutes may be divided by 60 to make it in hours.
You get the different in seconds if you subtract them, so divide it by 60 to get minutes and by 60 again to get hours.
I created this code to take standard PHP UNIX TIMESTAMP, calculate the difference in time and return a standard time or a specialized time format. This is great for timing a project and calculating the time it takes to get the results.
function timerFormat($start_time, $end_time, $std_format = false)
{
$total_time = $end_time - $start_time;
$days = floor($total_time /86400);
$hours = floor($total_time /3600);
$minutes = intval(($total_time/60) % 60);
$seconds = intval($total_time % 60);
$results = "";
if($std_format == false)
{
if($days > 0) $results .= $days . (($days > 1)?" days ":" day ");
if($hours > 0) $results .= $hours . (($hours > 1)?" hours ":" hour ");
if($minutes > 0) $results .= $minutes . (($minutes > 1)?" minutes ":" minute ");
if($seconds > 0) $results .= $seconds . (($seconds > 1)?" seconds ":" second ");
}
else
{
if($days > 0) $results = $days . (($days > 1)?" days ":" day ");
$results = sprintf("%s%02d:%02d:%02d",$results,$hours,$minutes,$seconds);
}
return $results;
}
Example:
$begin_routine_time = time();
echo(timerFormat($begin_routine_time, $time()));
$datetime1 = new DateTime(date('Y-m-d H:i:s', 1331875634));
$datetime2 = new DateTime(date('Y-m-d H:i:s'));
$oDiff = $datetime1->diff($datetime2);
echo $oDiff->y.' Years <br/>';
echo $oDiff->m.' Months <br/>';
echo $oDiff->d.' Days <br/>';
echo $oDiff->h.' Hours <br/>';
echo $oDiff->i.' Minutes <br/>';
echo $oDiff->s.' Seconds <br/>';
Once I needed to convert seconds to time like 1 day 03:34:13 days hours:minuts:secondes
I wrote this function
function sECONDS_TO_HMS($seconds)
{
$days = floor($seconds/86400);
$hrs = floor($seconds/3600);
$mins = intval(($seconds / 60) % 60);
$sec = intval($seconds % 60);
if($days>0){
//echo $days;exit;
$hrs = str_pad($hrs,2,'0',STR_PAD_LEFT);
$hours=$hrs-($days*24);
$return_days = $days." Days ";
$hrs = str_pad($hours,2,'0',STR_PAD_LEFT);
}else{
$return_days="";
$hrs = str_pad($hrs,2,'0',STR_PAD_LEFT);
}
$mins = str_pad($mins,2,'0',STR_PAD_LEFT);
$sec = str_pad($sec,2,'0',STR_PAD_LEFT);
return $return_days.$hrs.":".$mins.":".$sec;
}
echo sECONDS_TO_HMS(65); // 00:01:05
echo sECONDS_TO_HMS(76325); //21:12:05
echo sECONDS_TO_HMS(345872); // 4 Days 00:04:32
I think it could be helpful for you.
I have a variable called $final_time_saving which is just a number of minutes, 250 for example.
How can I convert that number of minutes into hours and minutes using PHP in this format:
4 hours 10 minutes
<?php
function convertToHoursMins($time, $format = '%02d:%02d') {
if ($time < 1) {
return;
}
$hours = floor($time / 60);
$minutes = ($time % 60);
return sprintf($format, $hours, $minutes);
}
echo convertToHoursMins(250, '%02d hours %02d minutes'); // should output 4 hours 17 minutes
echo date('H:i', mktime(0,257));
$hours = floor($final_time_saving / 60);
$minutes = $final_time_saving % 60;
You can achieve this with DateTime extension, which will also work for number of minutes that is larger than one day (>= 1440):
$minutes = 250;
$zero = new DateTime('#0');
$offset = new DateTime('#' . $minutes * 60);
$diff = $zero->diff($offset);
echo $diff->format('%a Days, %h Hours, %i Minutes');
demo
#Martin Bean's answer is perfectly correct but in my point of view it needs some refactoring to fit what a regular user would expect from a website (web system).
I think that when minutes are below 10 a leading zero must be added.
ex: 10:01, not 10:1
I changed code to accept $time = 0 since 0:00 is better than 24:00.
One more thing - there is no case when $time is bigger than 1439 - which is 23:59 and next value is simply 0:00.
function convertToHoursMins($time, $format = '%d:%s') {
settype($time, 'integer');
if ($time < 0 || $time >= 1440) {
return;
}
$hours = floor($time/60);
$minutes = $time%60;
if ($minutes < 10) {
$minutes = '0' . $minutes;
}
return sprintf($format, $hours, $minutes);
}
$t = 250;
$h = floor($t/60) ? floor($t/60) .' hours' : '';
$m = $t%60 ? $t%60 .' minutes' : '';
echo $h && $m ? $h.' and '.$m : $h.$m;
4 hours and 10 minutes
Sorry for bringing up an old topic, but I used some code from one of these answers a lot, and today I told myself I could do it without stealing someone's code. I was surprised how easy it was. What I wanted is 510 minutes to be return as 08:30, so this is what the code does.
function tm($nm, $lZ = true){ //tm = to military (time), lZ = leading zero (if true it returns 510 as 08:30, if false 8:30
$mins = $nm % 60;
if($mins == 0) $mins = "0$mins"; //adds a zero, so it doesn't return 08:0, but 08:00
$hour = floor($nm / 60);
if($lZ){
if($hour < 10) return "0$hour:$mins";
}
return "$hour:$mins";
}
I use short variable names because I'm going to use the function a lot, and I'm lazy.
The easiest way is :
gmdate('H:i', $numberOfSeconds * 60)
Just in case you want to something like:
echo date('G \h\o\u\r\s i \m\i\n\u\t\e\s', mktime(0, 90)); //will return 1 hours 30 minutes
echo date('G \j\a\m i \m\e\n\i\t', mktime(0, 90)); //will return 1 jam 30 menit
function hour_min($minutes){// Total
if($minutes <= 0) return '00 Hours 00 Minutes';
else
return sprintf("%02d",floor($minutes / 60)).' Hours '.sprintf("%02d",str_pad(($minutes % 60), 2, "0", STR_PAD_LEFT)). " Minutes";
}
echo hour_min(250); //Function Call will return value : 04 Hours 10 Minutes
$m = 250;
$extraIntH = intval($m/60);
$extraIntHs = ($m/60); // float value
$whole = floor($extraIntHs); // return int value 1
$fraction = $extraIntHs - $whole; // Total - int = . decimal value
$extraIntHss = ($fraction*60);
$TotalHoursAndMinutesString = $extraIntH."h ".$extraIntHss."m";
Thanks to #Martin_Bean and #Mihail Velikov answers. I just took their answer snippet and added some modifications to check,
If only Hours only available and minutes value empty, then it will display only hours.
Same if only Minutes only available and hours value empty, then it will display only minutes.
If minutes = 60, then it will display as 1 hour. Same if minute = 1, the output will be 1 minute.
Changes and edits are welcomed.
Thanks. Here is the code.
function convertToHoursMins($time) {
$hours = floor($time / 60);
$minutes = ($time % 60);
if($minutes == 0){
if($hours == 1){
$output_format = '%02d hour ';
}else{
$output_format = '%02d hours ';
}
$hoursToMinutes = sprintf($output_format, $hours);
}else if($hours == 0){
if ($minutes < 10) {
$minutes = '0' . $minutes;
}
if($minutes == 1){
$output_format = ' %02d minute ';
}else{
$output_format = ' %02d minutes ';
}
$hoursToMinutes = sprintf($output_format, $minutes);
}else {
if($hours == 1){
$output_format = '%02d hour %02d minutes';
}else{
$output_format = '%02d hours %02d minutes';
}
$hoursToMinutes = sprintf($output_format, $hours, $minutes);
}
return $hoursToMinutes;
}
2022 answer using Carbon
Carbon::createFromTime(
intdiv($final_time_saving, 60),
($final_time_saving % 60),
0,
0)
->format('H:i')
check this link for better solution. Click here
How to convert hh:mm:ss to minutes
$minutes=$item['time_diff'];
$hours = sprintf('%02d',intdiv($minutes, 60)) .':'. ( sprintf('%02d',$minutes % 60));