PHP Convert Minutes+Seconds to Seconds - php

Currently, I have the following time format like the following:
0m12.345s
2m34.567s
I put them in variable $time. How do I convert the variable to seconds only in PHP, like 12.345 for the first one, and 154.567 for the second one?

You can do it like this,
//Exploding time string on 'm' this way we have an array
//with minutes at the 0 index and seconds at the 1 index
//substr function is used to remove the last s from your initial time string
$time_array=explode('m', substr($time, 0, -1));
//Calculating
$time=($time_array[0]*60)+$time_array[1];

<?php
$time="2m34.567s";
$split=explode('m',$time);
// print_r($split[0]);
$split2=explode('s',$split[1]);
$sec= seconds_from_time($split[0])+$split2[0];
echo $sec;
function seconds_from_time($time) {
return $time*60;
}
?>
Demo here http://phpfiddle.org/main/code/gdf-3tj

Related

Integer value ignoring zeros and returning full number

I'm looking to create a PHP Range loop
In a first and second number in the range, but have noticed the first number which is
00006
Is being rounded down / flattened to show "6".
So when I echo the first number value I get "6" back. I need it to be "00006"
Then the next number will need to be 00007 and so on, via the range loop.
My PHP code at present is :
$first_number = 00006;
$last_number = 11807;
foreach(range($first_number, $last_number) as $id)
{
echo $id;
}
How do I go about making sure that the number has the previous 0's in it?
You can do it by using printf() function.
See the documentation : printf
First of all in PHP, a number starting with zero is treated as octal number, But I guess range() function converts it as decimal. So if you want to start it with 20. Like $first_number = 00020; then the output will be start with 16 not 20.
So, if you want the output starting with 0's, then you can do like this:
$first_number = 6;
$last_number = 11807;
foreach(range($first_number, $last_number) as $id)
{
printf("%05d",$id);
}

Highchart add three zeros in the loop

I have use library chart from this page link. Unfortunately, the data I download is not compatible, for example:
I get from JSON time:
time: 1346803200
this time is not displayed on the chart. I must add three zeros at the end (look like this: 1346803200000), then the chart displays correctly. So I have code:
for ($i=0; $i < count($chart['Data']) ; $i++) {
$time = $chart['Data'][$i]['time'];
}
I need add to variable $time numeric 000 (three zeros at the end). I can not add it this way:
$time = $chart['Data'][$i]['time']."000";
because variable $time change from int to string. I must have $time in integer type. Is there any way to add three zeros without changing the variable type inside the loop ?
Not sure why you are doing this or if there is a better way, but if type conversion is the only thing that worries you, you can explicitly cast it to int:
$time = (int)($chart['Data'][$i]['time']."000");
Also, not sure if this is your desired behavior, but just note that your $time variable will get overwritten with every iteration of the for loop.
And one more thing, you can achieve your desired output without the explicit conversion by just multiplying your result with 1000, like so:
$time = $chart['Data'][$i]['time'] * 1000;
This should be a better solution than concatenation when you are working with ints
Seriously?
$time = $chart['Data'][$i]['time'] * 1000;
You con multiply for 1000
$time = $chart['Data'][$i]['time']*1000;

Php check if digit time is greater than other digit time

I have an array of digit time numbers (I get them from the input fields).
Here are a few examples:
00:00:10
00:03:00
01:20:00
My question is following, how do I check if a digit time is greater than another?
Following function works up to
00:01:00
After that I get an error.
$inputTimes = $request->input('times', []);
foreach ($inputTimes as $inputKey => $inputValue)
{
$Input = new Input();
$Input->input_start = $inputValue['input_start'];
$tInput = explode(':', $inputValue['input_timelapse']);
$implInput = implode('', $tInput);
$iImplInput = (int)$implInput;
// Check if time is greater
if($iImplInput > $iVideoDuration)
{
.. error time greater
}
}
I would convert it to Unix time by using the inbuild strtotime() function.
For example:
strtotime('01:20:00'); will output 1483492800 while strtotime('00:01:00'); will output 1483488060.
Hope this helped.

Comparing datetimes in php not working

I have the following session value
$_SESSION["time"] = 2015-01-09 23:57:38 (example value)
And the variable $test
$test = time() - (60 * 1); (1 minute)
$test = date("Y-m-d H:i:s",$test);
What I want to do is check whether 1 minute as passed. To do this I have the following code:
if(isset($_SESSION["time"]) && (strtotime($_SESSION["time"]) > $test)){
echo "first";
}else{
echo "second";
}
Independently to which logic comparison I use (< or >), after a minute a page refresh still echoes the same... any idea on why? I'm finding this really strange...
Comparision should be like
($_SESSION["time"] > $test)
PHP now will compare two string values instead of integer (return value from strtotime call) and string (return value from date call)
What he actually wants is...
strtotime($_SESSION["time"]) > strtotime($test))

how to get all numbers from a string and create a total amount using php

i want to get the total amount of hours that is outputted from our database, but the problem is the values are on string format
ex of the values:
9H:20M
3H:13M
3H:50M
6H:30M
TOTAL:22H:53M
i want to get the total of all the values
and display it afterwards, but i don't know how to segregate the numbers from the string
and then convert it to time format, is there any way for me to achieve
the result i'm looking for using a php script for conversion and segregation of values and data?
Here is a pretty basic solution:
<?php
$times = array("9H:20M", "3H:13M", "3H:50M", "6H:30M",);
$total = 0;
foreach ($times as $time) {
preg_match("#(\d+)H:(\d+)M#", $time, $matches);
$total += 60 * $matches[1] + $mtaches[2];
}
echo $total;
This creates a value $total which contains the total number of minutes. Now you can convert back to H and M.
You can use strtotime or the newer DateTime class, but your current format is not compatible, so you have to some modification before. This is a possible solution:
<?php
$result = 0;
$times = array('9H:20M','3H:13M','3H:50M','6H:30M');
foreach($times as $time) {
preg_match('/^(\d{1,2})H[:]([0-5][0-9])M$/',$time, $m);
$timestamp = strtotime($m[1].':'.$m[2]);
$result+=$timestamp;
}
echo date('H:i',$result);
?>
I think the better approach for this is, to grep the total time directly from DB.

Categories