PHP Minutes to hours and minutes [duplicate] - php

This question already has answers here:
Convert number of minutes into hours & minutes using PHP
(14 answers)
Closed 6 years ago.
Is there any function I pass minutes and returns hours and minutes in PHP?
I have tried mktime function but not working more than 24 hours see bellow example
echo date('H:i', mktime(0,257));

Try this one
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(500);
this will return output : 08:20

You can use this:
$hours = floor($minutes / 60).':'.str_pad(($minutes % 60), 2, "0", STR_PAD_LEFT);

Related

PHP check if pass 5 minutes between two dates [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 5 years ago.
I am trying to check if pass 5 minutes between two dates.
Here is my code:
$time = strtotime("+5 minutes").'<br>';
$var = '1518219956';
$e1 = date("d-m-Y H:i",$time);
$e2 = date("d-m-Y H:i",$var);
if($var > $time){
echo 'true<br>';
echo 'Time: '.$e1.'<br> Var:'.$e2;
} else{
echo 'false<br>';
echo 'Time: '.$e1.'<br> Var: '.$e2;
}
I am sorry but I lost myself with this timestamps..
Sorry, I don't mean to be rude but I was confused by the question. What I designed here was to see if time B is five minutes after Time A.
A few notes:
No need to bother with strtotime or date. Just keep everything in unix time. Compare the seconds by using 60 * 5. 60 for 60 seconds in a minute and 5 for the number of minutes.
<?php
//Time A
$timeA = '1518223062';
//Time B (Which I set at the current time)
$timeB = time();
//five minutes in seconds
$fiveMinutes = 60 * 5;
//check if current time is after 5 minutes the initial time
if ( ($timeA+$fiveMinutes) <= $timeB) {
echo "True";
}
else {
echo "False";
}
?>

How to sum up time in php? [duplicate]

This question already has answers here:
PHP add up two time variables
(7 answers)
Closed 7 years ago.
How to sum up time in php.
For example I have this series of time duration logs:
00:10:00
00:30:10
01:00:50
The total should be 1 hour and 41 minutes
Here is my code:
$log_in = new DateTime($log->log_in);
$log_out = new DateTime($log->log_out);
$diff = $log_out->diff($log_in);
$total += strtotime($diff->format('%H:%i:%s'));
echo $diff->format('%H:%i:%s');
Convert the time into timestamp using strtotime() function. Then manipulate the time according your need and get result in terms of seconds.
Once you get seconds.
For Hour
$hour = $diff % 3600
For Minute
$Minute = ($diff - ( $hour *3600))%60;

Time difference php/mysql need to format the output conditionally (4 small highlights required)

im trying to get difference of date/time from a field type datetime to "right now" using php and mysql as database
this code is working fine, returns the output beautifully ok as required
$datetime1 = new DateTime('mydate1');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%d days %h hours %i minutes');
that is ok so far, no issues as this function is for php 5.3 and i have it on server
my need is 4 small things actually
1) how to eliminate the need for days?
i want to have (25 hours 10 minutes) instead of (1 day 1 hours 10 minutes)
2) how i can make $elapsed be bold or colored if the value is more than 5 hours for example!? simple IF logic will not work as the output is not actually a predefined value...
3) if the days or hours are 0, then want to remove them!
- For example if showing (0 days 10 hours 40 mins) then no need to display the (0 days), should show (10 hours 40 mins) that is enough
- Another example: 0 days 0 hours 45 minutes then to show only "45 minutes" no need for days and hours!
4) if output less than 5 minutes in total (0days 0 hours 1-5mins), then wanna make it show like "a while ago" only no need for any days, hours or minutes... then after 6 minutes.. go like "6 mins"
shortly something like facebook!?
okay, what i searched tried is different combination of workarounds but never worked as you know this interval is for php 5.3 and still seem not widely used?
any hint for one or more parts of this long question is appreciated,
M, Derik
tried to answer all your questions. so read the comments because i didnt write numbers for the problems. sorry
$datetime1 = new DateTime('mydate1');
$datetime2 = new DateTime();
$minutes = round(abs($datetime1 - $datetime2) / 60,2); //to calculate total time in MINUTES
if($minutes < 5) // for awhile ago problem.
{
return "awhile ago";
}
elseif($minutes > 6 && < 60)
{
return $minutes." minutes ago"; //for 6 minutes and after.
}
elseif($minutes>60) // for the hours..
{
$hours = floor($final_time_saving / 60);
$minutes = $final_time_saving % 60;
$string = $hours. " hours and" . $minutes . " minutes ago.";
if($hours>5) //for bolding characters after
{
return "<b>".$hours." hours and".$minutes." minutes ago.</b>"; //for bolding character
}
else
return $hours." hours and".$minutes." minutes ago.";
}
hope this helps.

Convert minutes to time 'h:m:s' [duplicate]

This question already has answers here:
Convert number of minutes into hours & minutes using PHP
(14 answers)
Closed 9 years ago.
I wonder if there is a function in php or codeigniter which can do this :
function transformTime($min)
{
$min=(int)$min;
$heure=(int)($min/60);
$minute=(($min/60)-$heure)*60;
return $heure .':' . $minute . ':00';
}
I want convert x minutes to a time format.
Do something like this
<?php
function transformTime($min)
{
$ctime = DateTime::createFromFormat('i', $min);
$ntime= $ctime->format('H:i:s');
return $ntime;
}
echo transformTime(60); // "Prints" 01:00:00
You are almost there, just format the output with sprintf() or str_pad():
function transformTime($min)
{
$hour = floor($min / 60);
$min -= $hour * 60;
return sprintf('%02d:%02d:00', $hour, $min);
}

Convert datetime into time ago [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to convert DateTime into the year, month, days, hours, minutes, seconds ago like as yahoo question asked 5week ago, or 1 year ago or 1 month ago, my date is saved in the database like this: 2011-11-30 05:25:50.
Now I want to show like year, month, days, hours, minutes and seconds in PHP like: how much year, how many months , days, hours, minutes, seconds ago, but need only one time unit show like it will be year or days or hours or minutes or seconds.
I have searched about this but not understand how I can do this, I know there are many questions at this topic on Stack Overflow, maybe it is duplicate but I am unable to solve my issue.
Thanks in advance.
Assuming you mean relative to the present (if you don't, please clarify your question), you could use this:
$then = new DateTime('2011-11-30 05:25:50');
$now = new DateTime();
$delta = $now->diff($then);
$quantities = array(
'year' => $delta->y,
'month' => $delta->m,
'day' => $delta->d,
'hour' => $delta->h,
'minute' => $delta->i,
'second' => $delta->s);
$str = '';
foreach($quantities as $unit => $value) {
if($value == 0) continue;
$str .= $value . ' ' . $unit;
if($value != 1) {
$str .= 's';
}
$str .= ', ';
}
$str = $str == '' ? 'a moment ' : substr($str, 0, -2);
echo $str;
Output:
1 year, 9 months, 17 days, 14 hours, 31 minutes, 31 seconds
I've always used this function:
function when($datetime) {
define("SECOND", 1);
define("MINUTE", 60 * SECOND);
define("HOUR", 60 * MINUTE); define("DAY", 24 * HOUR);
define("MONTH", 30 * DAY); $delta = time() - strtotime($datetime);
// convert
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"; }
}
echo when(YOUR DATE HERE) will return the best format of relative time, which might be days, hours, or if it wasn't that long ago, even in seconds.
Try this:
http://www.php.net/manual/en/function.strtotime.php
PHP has a nice function strtotime all ready for your needs.
That'll give you a timestamp, then you can just do math from there from whatever date you are subtracting from.
You could try splitting this using preg_split.
Try something like
$output = preg_split( "/ (-| |:) /", $input );
That should give you an array, where the first element is the year, second is the month, and so on.

Categories