sc_time_diff

script not works, and obtain only Array not value:

$time = sc_time_diff({start_time}, ‘hh-mm-ss’, {end_time}, ‘hh-mm-ss’);

sc_alert($time);

sc_time_diff

sc_time_diff

script not works, and obtain only Array not value:

$time = sc_time_diff({start_time}, ‘hh-mm-ss’, {end_time}, ‘hh-mm-ss’);

sc_alert($time);

According to the documentation:
“The result will be returned in an array, with the dimensions [0], [1] and [2] containing hours, minutes and seconds, respectively.”

You need to do something like:
$hours = $time[0];
sc_alert($hours);

jsb

ok…works…thanks…

but if i want add minute , i try but not works , $hours = $time[0,1];

$newtime = $time[0].’:’.$time[1];

jsb

[QUOTE=jsbinca;36560]$newtime = $time[0].’:’.$time[1];

jsb[/QUOTE]

It is created the time as 1:30, but now use the field int e’mi by mistake I have to use varchar field? I would then add the results

Sorry, but I’m not quite sure what exactly you want to do.
The result of the sc_time_diff() macro gives you the time difference in an array with 3 ‘fields’, $time[0] = hours, $time[1] = minutes, $time[2] = seconds, as plain integer numbers.

If you want to add some minutes (before showing it) you can just use
$time[1] += 5; //this would add 5 minutes, but be advised, it does not carry over the hour at 60!

If you want a correct time calculation you have to bring it in a time format.

$newtime = $time[0].’:’,$time[1]; // As you noticed, this gives you the time in hours:minutes as a string, in order to be human readable.

Now, if you want to add or subtract some minutes you have to convert it to a machine readable format. One solution is:

$newtime = strtotime($newtime) + 120; //this adds 2 minutes (120 = 2 minutes x 60 seconds)

Finally convert it back to human readable.
$newtime = date(‘h:i’,$newtime); // this gives you ‘1:32’;

The final statement would be:
$newtime = date(‘h:i’, strtotime($newtime) + 120);

As I said, this is one solution, for more options please read there:
http://php.net/manual/en/book.datetime.php

jsb

1 Like

I am new to scriptcase and have a related question.
I have date and time in a variable {time] stored in Date and Time (SQL DATETIME) format.
I need to extract hour but there is no macro similar to the sc_date_conv macro which works so well to extract month, day and year. I am trying to use sc_time_diff to calculate the difference with time = 00:00:00 to extract hour as $timearray[0] but have been unsuccessful.

Here is the code I have
$timearray = sc_time_diff(“01/24/2016 00:00:00”,“MM/dd/yyyy HH:mm:ss”,{time}, “db_format”); //I am assuming that the DATETIME format requires me to have both date and time but I have tried time only

{hour_int} = $timearray[0]; // hoping to have hour as an integer returned from $timearray[0]

Thanks for any help