iqra
iqra

Reputation: 115

converting H:i:s to minutes only but array_map() giving errors

I am trying to convert a long datatype data to time in which I am successful. In each session time array I have values like ["1276999","787878","677267"]. I passed this array in the array_map function and converted to time which is working.

Now within the the convert_time function I am calling another function, using array_map which will convert each time (i.e 1:40:00 to 100 minutes) but the issue is my 2nd array map function which is giving me error that array_map needs 2nd parameter to be an array...

$each_session_time = array();

for ($i=0; $i<sizeof($array_in_time_str) ; $i++) { 
   $each_session_time[$i]=$array_out_time_str[$i]-$array_in_time_str[$i];
}

//session time in hours
array_map("convert_to_time", $each_session_time);

function convert_to_time($each_session) {
   # code...
   $each_sess_time=array();
   $each_sess_time=date("H:i:s",$each_session);
   array_map("get_minutes",$each_sess_time);
   return $each_sess_time;
}

function get_minutes($session_time) {
   // algorithm to convert each session time to minutes)
 }

Upvotes: 0

Views: 154

Answers (3)

Bartłomiej Wach
Bartłomiej Wach

Reputation: 1986

considering you are working with strings like "XX:YY:ZZ" you can try

$split = explode(":",$session_time); $minutes = $split[1];

to get the "i" part of the string.

You could also use a dateTime object (http://php.net/manual/en/class.datetime.php) by doing new DateTime($each_session); in the first loop and using DateTime::format("H:i:s") and DateTime::format("i") on that object depending on what data you need

Upvotes: 1

HTMHell
HTMHell

Reputation: 6006

You need to move out the array_map("get_minutes",$each_session_time); from the convert_to_time function.

example:

<?php
$each_session_time=["1276999","787878","677267"];

//session time in hours
$times = array_map("convert_to_time", $each_session_time);
$minutes = array_map("get_minutes",$times);

function convert_to_time($each_session)
{
    # code...
    $each_sess_time=array();
    $each_sess_time=date("H:i:s",$each_session);

    return $each_sess_time;
}

function get_minutes($session_time)
{
    //algo to convert each session time to minutes)
}

print_r($minutes);

Upvotes: 1

jeroen
jeroen

Reputation: 91744

It seems you are starting with valid timestamps - seconds passed since January 1, 1970 - so to get the difference between two values in minutes, you just have to subtract one from the other and multiply it by 60.

If you want more control over your data, for example to format it differently later on, I would recommend using DateInterval objects instead of the difference between two timestamps and strings that you are using now. Note that the difference between two timestamps is not a valid timestamp itself so you cannot use date() to format it.

Upvotes: 1

Related Questions