/    Sign up×
Community /Pin to ProfileBookmark

In the code below,

array contains status and timestamp.
If status is all zero then the difference of time is calculated. and if status changes from 0 to 1 again timestamp is calculated.

Now here in the array after timestamp 2008-06-13 00:50:00 to end of the array if status is 0 then timestamp is not calculated.

Please tell me how to change in the code below.

[code]
<?php
$values=array(
“0#2008-06-13 00:10:00”,
“0#2008-06-13 00:20:00”,
“0#2008-06-13 00:30:00”,
“1#2008-06-13 00:40:00”,
“0#2008-06-13 00:50:00”,
“0#2008-06-13 01:10:00″
);

$i=0;
$outset = array();
$status = array();
$aset = 0;

foreach($values as $v) //For each element in the values array
{
// Split the value by hash
$vals = explode(‘#’, $v);
// If this is the first loop, set the previous status
if($i == 0) {$previous_value = $vals[0]; $i=1; $starttime = $vals[1];}

// If the previous =0 and the current is 1, store the values in a new array for output
if($previous_value == 0 && $vals[0] == 1)
{
$aset = 1;
$tdiff = strtotime($vals[1]) – strtotime($starttime);
$outset[] = $starttime.’ ‘.$vals[1].’ ‘.$tdiff;
}

if($vals[0] == 0 && $previous_value == 1)
{
$starttime = $vals[1];
}

// Store the current, for the next loop it is previous value
$previous_value = $vals[0];
$lasttime = $vals[1];
}

if($aset == 0)
{
$tdiff = strtotime($vals[1])-strtotime($starttime);
$outset[] = $starttime.’ ‘.$lasttime.’ ‘.$tdiff;
}

echo”<pre>”;
print_r($outset);
echo”</pre>”;
?>
[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@SheldonJun 16.2008 — 30 seconds or play... not sure if it will work at all..

[code=php]

<?php

$values=array(
"0" => "2008-06-13 00:10:00",
"1" => "2008-06-13 00:20:00",
"0" => "2008-06-13 00:30:00",
"1" => "2008-06-13 00:40:00",
"0" => "2008-06-13 00:50:00",
"0" => "2008-06-13 01:10:00"
);


foreach($values as $key => $time){

if($i === 0){
$previous_value = $key;
$i=1;
$starttime = $time;
}

if(($previous_value === 0) and ($key === 1)){
$aset = 1;
$tdiff = strtotime($key) - strtotime($starttime);
$outset .= "{$starttime} {$time} {$tdiff}";
}

if(($key === 0) and ($previous_value === 1)){
$starttime = $time;
}

$previous_value = $key;
$lasttime = $time;
}

if($aset === 0){
$tdiff = strtotime($key)-strtotime($starttime);
$outset .= "{$starttime} {$lasttime} {$tdiff}";
}


echo"{$st}<pre>";
echo("{$outset}<br />");
echo"</pre>";

?>[/code]
×

Success!

Help @rashmi_k28 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...