How to convert time to hours only (php)? [duplicate] - php

This question already has an answer here:
Convert Time String to Decimal Hours PHP [closed]
(1 answer)
Closed 8 years ago.
I need little function in PHP which will convert hour:minutes only to hours.
a.g.
15:30 = 15.5
15:15 = 15.25
15:45 = 15.75
and so on
Thank you guys

This should work for you:
<?php
function timeToDecimal($time) {
$hm = explode(":", $time);
return ($hm[0] + ($hm[1]/60));
}
$time = "15:15";
echo timeToDecimal($time);
?>
Output:
15.25

You could do it this way:
function toDecimalTime($time)
{
$parsed = date_parse($time);
return $parsed["hour"] + $parsed["minute"] / 60;
}

Related

How to get 1 hours before [duplicate]

This question already has answers here:
Create Variable in PHP Equal to Current Time Minus One Hour
(8 answers)
Closed 3 years ago.
how to get 1 hours before in php
I try with:
but not working
$jam = "10:00:00";
$1hrsbefore = strtotime($jam), strtotime(-1 hours);
Try this code :
$jam = "10:00:00";
$timeadd= strtotime($jam) - 60*60;
$time = date('H:i:s', $timeadd);
echo $time;
Try this
$jam="10:00:00";
$time = date('H:i:s', strtotime($jam) - 60*60);
echo $time;

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;

How to calculate interval in 30 minutes between two dates? [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 8 years ago.
I have for example:
$first = '2014-05-18 12:00:00';
$last = '2014-05-18 13:30:00';
and i would like calculate interval in 30 minutes for these dates.
For this example interval = 3.
Other example:
$first = '2014-05-18 11:00:00';
$last = '2014-05-18 14:00:00';
interval = 6;
How is the best way for this in PHP?
Simple math solution
$s=floor(abs(strtotime($first)-strtotime($last))/30/60);
Here is fiddle
https://eval.in/153120
function timeDiff30minutes($first,$last){
$first = new Datetime($first);
$last = new Datetime($last);
$time_diff = $first->diff($last);
$time_diff_minute = $time_diff->days * 24 * 60;
$time_diff_minute += $time_diff->h * 60;
$time_diff_minute += $time_diff->i;
return $time_diff_minute/30;
}
timeDiff30minutes('2014-05-18 11:00:00','2014-05-18 14:00:00') // output 6

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);
}

Calculate date interval in PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Subtracting dates in PHP
I have two Unix timestamps, how can I calculate the total number of days between them?
$timestamp1 = x;
$timestamp2 = y;
$days_elapsed = floor(($timestamp2 - $timestamp1)/86400);
echo $days_elapsed;
Convert them to UNIX-timestamp (if they arent already), then just
$diff = abs($timestamp1 - $timestamp2);
$days = (int) ($diff / 60 / 60 / 24);
something like this should do the trick:
$days = (strtotime($timestamp1)-strtotime($timestamp2))/(60*60*24);
This code should do the trick
$numDays = abs($timeOne - $timeTwo)/60/60/24;

Categories