0

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)
 }
fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44
iqra
  • 115
  • 1
  • 12
  • seems like you don't need `array_map` since you're only interested in converting a single value at once. Call `get_minutes($each_sess_time)` directly – Alex Tartan Aug 24 '16 at 07:35
  • `array_map("get_minutes",$each_sess_time)` Here `$each_sess_time` is a single value. What for are you using `array_map` here? – u_mulder Aug 24 '16 at 07:36

3 Answers3

1

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.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • @jeron i know kindly help me in ameliorating the code as i am novice in php development – iqra Aug 24 '16 at 07:38
  • @iqra it's not really clear what you want, you are not doing anything with the result of your function calls. What is your input and what do you want as output? – jeroen Aug 24 '16 at 07:40
  • ok @jeoren letme elaborate the things i am trying to achieve for you – iqra Aug 24 '16 at 07:48
  • @iqra Don't you already have the answer you were looking for? You marked one as accepted... – jeroen Aug 24 '16 at 07:53
  • @jeoren i am building an employee attendance system which will keep an account of employee timings in the office in my mysql table i have a table of employee_timings from which i am fetching the values of emp_in_time and emp_out_time forexample an employee enters the office at 11:00:00 and leaves at 13:00:00 then on the same day he again comes to office at 15:00:00 and leaves at 18:00:00 i m calculating the total time of each session like for instance that he spent 3hrz in the first session and 4hrz in the second respectively.. – iqra Aug 24 '16 at 07:54
  • @jeoren then i m calculating the accumulative time of all the session against thats particular date of that specific employee... – iqra Aug 24 '16 at 07:55
  • @jeoren now the question is why would i want to convert time to minutes only forexample 1:50:00 to 110 minutes that is because i m showing my result on bar graph which has time starting from 09:00:00 and ending at 18:00:00 on y-axis and on x-axis i have minutes starting from 0 10 20 30 40 50 60 – iqra Aug 24 '16 at 08:00
  • @jeoren now after converting 1:50:00 to 110 minutes in will divide these minutes to 60 60 50 and then pass these values to data of bar graph against respective timings – iqra Aug 24 '16 at 08:05
  • @jeoren yes i got the answer but since am not getting the correct time against time-stamps and you pointed that too so i was thinking of getting your help .Thankx – iqra Aug 24 '16 at 08:06
1

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);
HTMHell
  • 5,761
  • 5
  • 37
  • 79
1

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

Bartłomiej Wach
  • 1,968
  • 1
  • 11
  • 17