FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Counting the duration in PHP
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Counting the duration in PHP [message #170698] Mon, 15 November 2010 12:33 Go to next message
venky_lb is currently offline  venky_lb
Messages: 4
Registered: November 2010
Karma: 0
Junior Member
Hi all,
I have a csv file from where i read the datas and show them on my
UI, the data would be something like "66:23:12" (HH:MM:SS).
The task is now to increment the duration manually and show them in
the UI until the csv is been updated.

$time_str_parts = explode(':', "66:23:12"); //duration from the csv
file
$chng_hour = $time_str_parts[0];
$chng_min = $time_str_parts[1];
$chng_secs = $time_str_parts[2] + 1;

if ($time_str_parts[2] >= 58) {
$chng_hour = $time_str_parts[0] + 1;

if ($time_str_parts[0] >= 58) {
$chng_min = $time_str_parts[1] + 1;
}
}
$cL = $chng_hour . ':' . $chng_min . ':' . $chng_secs;
echo $cL;

Help,suggestions and queries appreciated.

Thanks
Re: Counting the duration in PHP [message #170700 is a reply to message #170698] Mon, 15 November 2010 13:12 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 11/15/2010 1:33 PM, venky_lb wrote:
> Hi all,

Hello Venk,

> I have a csv file from where i read the datas and show them on my
> UI, the data would be something like "66:23:12" (HH:MM:SS).
> The task is now to increment the duration manually and show them in
> the UI until the csv is been updated.
>

So that string "66:23:12" is a duration of something?
Meaning 66 hours, 23 minutes, 12 seconds.
Right?


> $time_str_parts = explode(':', "66:23:12"); //duration from the csv
> file
> $chng_hour = $time_str_parts[0];
> $chng_min = $time_str_parts[1];
> $chng_secs = $time_str_parts[2] + 1;

What is that "+1" over there?
Does that mean you want to add 1 second to the duration?

>
> if ($time_str_parts[2]>= 58) {
> $chng_hour = $time_str_parts[0] + 1;

That is wrong: Why increase the hour if the seconds are > 57?

>
> if ($time_str_parts[0]>= 58) {
> $chng_min = $time_str_parts[1] + 1;
> }
> }


And why 58?
You code ">= 58" but that is also true for 59.
And 59 is a perfectly acceptable value for seconds and minutes (or hours).

> $cL = $chng_hour . ':' . $chng_min . ':' . $chng_secs;
> echo $cL;
>
> Help,suggestions and queries appreciated.

The way I would approach it is like:
Assuming that the following function takes the original duration and an
added duration, both expressed as "HH:MM:SS" like you did above.

Here is the idea (without errorhandling. I simply expect that the data
is in the right format. Don't use like this in any production environment.)

function addDuration($orgDuration,$addedDuration){
list($Oh,$Om,$Os) = explode(":",$orgDuration);
list($Ah,$Am,$As) = explode(":",$addedDuration);

$newSec = (int)$Os + (int)$As;
$newMinute = (int)$Om + (int)$Am;
$newHour = (int)$Oh + (int)$Ah;
if ($newSec > 59){
$newSec = $newSec - 60;
$newMinute = $newMinute + 1;
}

if ($newMinute > 59){
$newMinute = $newMinute - 60;
$newHour = $newHour + 1;
}

// pad some zeroes 0 to the output if you
// want to return "87:04:05" instead of "87:4:5"
return "$newHour:$newMinute:$newMinute";

}

Regards,
Erwin Moller


>
> Thanks


--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
Re: Counting the duration in PHP [message #170701 is a reply to message #170698] Mon, 15 November 2010 13:14 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 11/15/2010 7:33 AM, venky_lb wrote:
> Hi all,
> I have a csv file from where i read the datas and show them on my
> UI, the data would be something like "66:23:12" (HH:MM:SS).
> The task is now to increment the duration manually and show them in
> the UI until the csv is been updated.
>
> $time_str_parts = explode(':', "66:23:12"); //duration from the csv
> file
> $chng_hour = $time_str_parts[0];
> $chng_min = $time_str_parts[1];
> $chng_secs = $time_str_parts[2] + 1;
>
> if ($time_str_parts[2]>= 58) {
> $chng_hour = $time_str_parts[0] + 1;
>
> if ($time_str_parts[0]>= 58) {
> $chng_min = $time_str_parts[1] + 1;
> }
> }
> $cL = $chng_hour . ':' . $chng_min . ':' . $chng_secs;
> echo $cL;
>
> Help,suggestions and queries appreciated.
>
> Thanks

You can't do this reliably in PHP. PHP is server side, and you can't
guarantee when data will be displayed by the browser. For instance, it
may be displayed as you send it - but more likely it will be buffered by
php, the web server, the server's OS and/or the browser. You can
control the php buffering with flush(), but you can't control the rest.

If you need something like this, you'll have to go to a client-side
language such as javascript, flash or the like.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Counting the duration in PHP [message #170702 is a reply to message #170701] Mon, 15 November 2010 13:21 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 11/15/2010 2:14 PM, Jerry Stuckle wrote:
> On 11/15/2010 7:33 AM, venky_lb wrote:
>> Hi all,
>> I have a csv file from where i read the datas and show them on my
>> UI, the data would be something like "66:23:12" (HH:MM:SS).
>> The task is now to increment the duration manually and show them in
>> the UI until the csv is been updated.
>>
>> $time_str_parts = explode(':', "66:23:12"); //duration from the csv
>> file
>> $chng_hour = $time_str_parts[0];
>> $chng_min = $time_str_parts[1];
>> $chng_secs = $time_str_parts[2] + 1;
>>
>> if ($time_str_parts[2]>= 58) {
>> $chng_hour = $time_str_parts[0] + 1;
>>
>> if ($time_str_parts[0]>= 58) {
>> $chng_min = $time_str_parts[1] + 1;
>> }
>> }
>> $cL = $chng_hour . ':' . $chng_min . ':' . $chng_secs;
>> echo $cL;
>>
>> Help,suggestions and queries appreciated.
>>
>> Thanks
>
> You can't do this reliably in PHP. PHP is server side, and you can't
> guarantee when data will be displayed by the browser. For instance, it
> may be displayed as you send it - but more likely it will be buffered by
> php, the web server, the server's OS and/or the browser. You can control
> the php buffering with flush(), but you can't control the rest.
>
> If you need something like this, you'll have to go to a client-side
> language such as javascript, flash or the like.
>

I thought he asking HOW to add it, and serve that updated CSV.
But now I reread it I am not sure what he is asking anymore. ;-)

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
Re: Counting the duration in PHP [message #170703 is a reply to message #170702] Mon, 15 November 2010 14:24 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 11/15/2010 8:21 AM, Erwin Moller wrote:
> On 11/15/2010 2:14 PM, Jerry Stuckle wrote:
>> On 11/15/2010 7:33 AM, venky_lb wrote:
>>> Hi all,
>>> I have a csv file from where i read the datas and show them on my
>>> UI, the data would be something like "66:23:12" (HH:MM:SS).
>>> The task is now to increment the duration manually and show them in
>>> the UI until the csv is been updated.
>>>
>>> $time_str_parts = explode(':', "66:23:12"); //duration from the csv
>>> file
>>> $chng_hour = $time_str_parts[0];
>>> $chng_min = $time_str_parts[1];
>>> $chng_secs = $time_str_parts[2] + 1;
>>>
>>> if ($time_str_parts[2]>= 58) {
>>> $chng_hour = $time_str_parts[0] + 1;
>>>
>>> if ($time_str_parts[0]>= 58) {
>>> $chng_min = $time_str_parts[1] + 1;
>>> }
>>> }
>>> $cL = $chng_hour . ':' . $chng_min . ':' . $chng_secs;
>>> echo $cL;
>>>
>>> Help,suggestions and queries appreciated.
>>>
>>> Thanks
>>
>> You can't do this reliably in PHP. PHP is server side, and you can't
>> guarantee when data will be displayed by the browser. For instance, it
>> may be displayed as you send it - but more likely it will be buffered by
>> php, the web server, the server's OS and/or the browser. You can control
>> the php buffering with flush(), but you can't control the rest.
>>
>> If you need something like this, you'll have to go to a client-side
>> language such as javascript, flash or the like.
>>
>
> I thought he asking HOW to add it, and serve that updated CSV.
> But now I reread it I am not sure what he is asking anymore. ;-)
>
> Regards,
> Erwin Moller
>

Agreed - I'm a bit confused, also. But since he's adding one second, I
suspect he's trying to loop on the server. It wouldn't make a lot of
sense to add one second then just display the page.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: Website Creation Tutorial for Beginners
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 02:36:51 GMT 2024

Total time taken to generate the page: 0.02362 seconds