I am a little array-ed out with this and I am hoping that someone can help me. I have a few variables and am a little stuck with using the arrays in an if statement.
What I like to find out is if my relocation_date is less then notified_time (from a generated series) then give me old_meter_id else meter_id
$meter_id = 1393;
$notified_time = '2013-05-01 09:53';
$completed_time = '2013-05-01 11:52';
$relocation_date = '2013-04-24 00:00';
$old_meter_id = 1832;
$notified_time = strtotime($notified_time);
$completed_time = strtotime($completed_time);
$relocation_date = date($relocation_date);
$combined = array();
for($i=0;$i<=10;$i++)
{
$start_series = strtotime("- $i weeks", $notified_time);
$end_series = strtotime("- $i weeks", $completed_time);
$combined[] = array( 'start_time' => date('d-m-Y H:i:s',$start_series),
'end_time' => date('d-m-Y H:i:s',$end_series),
'relocation' => $relocation_date,
'meter_id' => $meter_id,
'old_meter_id' => $old_meter_id,
);
}
sample expected output to be like:
Array
(
[0] => Array
(
[start_time] => 01-05-2013 09:53:00
[end_time] => 01-05-2013 11:52:00
[relocation] => 2013-04-24 00:00
[meter_id] => 1393
)
[1] => Array
(
[start_time] => 24-04-2013 09:53:00
[end_time] => 24-04-2013 11:52:00
[relocation] => 2013-04-24 00:00
[meter_id] => 1832
)
.....
Thanks in advance!
You can check it in the loop like
for($i=0;$i<=10;$i++)
{
$start_series = strtotime("- $i weeks", $notified_time);
$end_series = strtotime("- $i weeks", $completed_time);
if(strtotime($relocation_date) < $start_series){
$combined[] = array( 'start_time' => date('d-m-Y H:i:s',$start_series),
'end_time' => date('d-m-Y H:i:s',$end_series),
'relocation' => $relocation_date,
'meter_id' =>$old_meter_id,
);
}else{
$combined[] = array( 'start_time' => date('d-m-Y H:i:s',$start_series),
'end_time' => date('d-m-Y H:i:s',$end_series),
'relocation' => $relocation_date,
'meter_id' =>$meter_id,
);
}
}
Hope this is the solution you are looking for
Related
Below is the array inside loop:
I want to filter this array by [date-begin] and [date-end]
For eg if I post startdate = 2015-06-29 and enddate = 2015-08-29
then array data between this range should come.
I tried:
1. array_slice
2.foreach(range ($startdate,$enddate) as $data){
echo "Age: {$data}<br />";
}
Array
(
[name] => MCLE 201
[date-begin] => 2015-06-29
[date-end] => 2015-06-29
)
Array
(
[name] => MCLE 201
[date-begin] => 2015-07-29
[date-end] => 2015-07-29
)
Array
(
[name] => MCLE 201
[date-begin] => 2015-08-29
[date-end] => 2015-08-29
)
Array
(
[name] => MCLE 201
[date-begin] => 2015-09-29
[date-end] => 2015-09-29
)
#Aashi you can do it with foreach() like below:
<?php
$yourArray = array(
array(
"name" => "MCLE 201",
"date-begin" => "2015-06-29",
"date-end" => "2015-06-29"
),
array(
"name" => "MCLE 201",
"date-begin" => "2015-07-29",
"date-end" => "2015-07-29"
),
array(
"name" => "MCLE 201",
"date-begin" => "2015-08-29",
"date-end" => "2015-08-29"
),
array(
"name" => "MCLE 201",
"date-begin" => "2015-09-29",
"date-end" => "2015-09-29"
)
);
$startdate = "2015-06-29";
$enddate = "2015-08-29";
$filteredArr = array();
foreach($yourArray as $value) {
if($startdate <= $value["date-begin"] && $enddate >= $value["date-end"]){
$filteredArr[] = $value;
}
}
echo "<pre>";
print_r($filteredArr);
Try this:
$filterArray = array();
foreach($arr as $key=>$val){
if(strtotime($val['date-begin']) >= strtotime($postedDateBegin) && strtotime($val['date-end']) <= strtotime($postedDateEnd)){
$filterArray[] = $val;
}
}
Click here to check output
this is proper solution for you question !
function date_is_between($start_date, $end_date, $date){
$start_date = date('Y-m-d',strtotime($start_date));
$end_date = date('Y-m-d',strtotime($end_date));
$date = date('Y-m-d',strtotime($date));
$match = FALSE;
if (($date => $start_date) && ($date <= $end_date))
$match = TRUE;
return $match;
}
function date_between($element, $start_date, $end_date)
{
$match = FALSE
if(date_is_between($element['date-begin'], $element['date-end'], $element['date-begin']) && date_is_between($element['date-begin'], $element['date-end'], $element['date-end']))
$match = TRUE;
return $match;
}
$filter_array = array_filter($data, "date_between");
I have this set of unix timestamps:
1473804000 1471831200
I'm not able to assign a date in this format ('Y-m-d H:i:s') to each unix timestamp.
My code:
$eventhour = '1473804000';
$client = array(
'pt' => $pt,
'eventhour' => date('Y-m-d H:i:s', $eventhour),
'activepower' => $avg['system.avg(activepower)']
);
The eventhour key is empty, I guess the function date returned false. The unix timestamp is valid, why is it returning false?
My Loop code
foreach ($result as $row):
$pt = $row['pt'];
$statement = new \Cassandra\SimpleStatement("select avg(activepower) FROM datavalue_test_by_hour WHERE pt= :pt AND eventhour = :eventhour;");
$options = new \Cassandra\ExecutionOptions(
array('arguments' => array('pt' => $pt, 'eventhour' => $row['eventhour'])))
;
$data = $this->cassandra->executeWithOptions($statement, $options);
$eventhour = $row['eventhour'];
echo $eventhour . PHP_EOL;
$avg = $data->first();
$client = array(
'pt' => $pt,
'eventhour' => date('Y-m-d H:i:s', $eventhour),
'activepower' => $avg['system.avg(activepower)']
);
print_r($client);
//fputcsv($fp, $client);
endforeach;
My print_r():
1462554000
Array
(
[pt] => AFH0AEFB0BFDEI
[eventhour] =>
[activepower] => 0.02475
)
1474887600
Array
(
[pt] => A0A0AEAF0DI0H0
[eventhour] =>
[activepower] => 0.1115
)
1475244000
Array
(
[pt] => AFH0ADE0AB0GDI
[eventhour] =>
[activepower] => 0.0905
)
1473008400
Array
(
[pt] => A0A0AEAF0GDFHB
[eventhour] =>
[activepower] => 0.014
)
1470693600
Array
(
[pt] => AFH0AEE00AF0FG
[eventhour] =>
[activepower] => 0.051
)
1452200400
Array
(
[pt] => AFH0AEE00BACIC
[eventhour] =>
[activepower] => 0.152
)
1463637600
Array
(
[pt] => AFH0ACFB00CD0F
[eventhour] =>
[activepower] => 0.041
)
I resolved this issue by casting the returned $row['eventhour'] from the database to a INT.
$eventhour = (int) $row['eventhour'];
Is working now.
I am getting nested array reply. It is contain date, time and day I want to break that. How can i do that.
This is my reply
Array
(
[code] => 202
[message] => Accepted
[data] => Array
(
[result] => Array
(
[15:45~31-10-2016 Mon] => Array
(
[Sday] =>
[Ttime] => 15:45
[Smonth] =>
"[15:45~31-10-2016 Mon] => Array" how to assign variable and how can i break this into day ,date and time variable
As mentioned in Mohammad's comment on your question,
You should use preg_split function.
$str = key($array['data']['result']); // 15:45~31-10-2016 Mon
$res = preg_split("/[~\s]/", $str);
echo '<pre>'; print_r($res);
output:-
Array
(
[0] => 15:45
[1] => 31-10-2016
[2] => Mon
)
If you have only single element in result array, use extract and explode to retrieve the values from man array: Something like -
$result = $array['data']['result'];
$date = key($result);
$day = extract($result[$date]);
var_dump($date); // 15:45~31-10-2016 Mon
var_dump($Ttime); // will output 15:45
$date = explode(' ', $date);
$dateString = substr($date[0], strpos($date[0], "{$Ttime}~") + 1); //31-10-2016
$week = $date[1]; // var_dump($week); will give `Mon`
Given:
$result = array(
'code' => '202',
'message' => 'Accepted',
'data' => array(
'result' => array(
'15:45~31-10-2016 Mon' => array(
'Sday' => '',
'Ttime' => '15:45',
'Smonth' => ''
)
)
)
);
you can do this:
$data = $result['data']['result'];
$dateKey = key($data);
$dateString = preg_split("/[~\s]/", $dateKey);
$date = array(
'day' => $dateString[2],
'date' => $dateString[1],
'time' => $dateString[0]
);
var_dump($date);
or this:
$data = $result['data']['result'];
$dateKey = key($data);
$dateString = preg_replace("/[~\s]/", ' ', $dateKey);
$dateObj = DateTime::createFromFormat('H:i d-m-Y D', $dateString);
$date = array(
'day' => $dateObj->format('D'),
'date' => $dateObj->format('m-d-Y'),
'time' => $dateObj->format('H:i')
);
var_dump($date);
I have an array in php like this
Array (
[0] => Array (
[date] => 21/07/2014
[total_booking] => 1
)
[1] => Array (
[date] => 1/08/2014
[total_booking] => 1
)
[2] => Array (
[date] => 2/09/2014
[total_booking] => 2
)
)
if i get value $_POST['from_date'] and $_POST['to_date'] all array other than between this from_date,to_date should be removed.
Edit my code
foreach ($newarray as $newarray){
if ($newarray[Date] < $to_date && $from_date > $newarray[Date])
{
// i want to remove this row from array
}
}
Here's a solution using array_filter:
<?php
$data = array(
array(
'date' => '21/07/2014',
'total_booking' => '1'
),
array(
'date' => '1/08/2014',
'total_booking' => '1'
),
array(
'date' => '2/09/2014',
'total_booking' => '2'
)
);
$from_date = strtotime('01-08-2014'); //$_POST['from_date']
$to_date = strtotime('21-08-2014'); //$_POST['to_date']
$data = array_filter($data, function($array) use ($from_date, $to_date) {
$epoch = strtotime(str_replace('/', '-', $array['date']));
return $epoch >= $from_date && $epoch <= $to_date;
});
$data = array_values($data);
print_r($data);
Output:
Array
(
[0] => Array
(
[date] => 1/08/2014
[total_booking] => 1
)
)
Only one element is outputted because only 1/08/2014 is between 1/08/2014 and 21/08/2014.
For earlier PHP versions:
function filter($array) {
$from_date = strtotime('01-08-2014'); //$_POST['from_date']
$to_date = strtotime('21-08-2014'); //$_POST['to_date']
$epoch = strtotime(str_replace('/', '-', $array['date']));
return $epoch >= $from_date && $epoch <= $to_date;
}
$data = array_filter($data, 'filter');
[0] => 12:00:00
[1] => 12:15:00
[2] => 12:30:00
[3] => 12:45:00
[5] => 13:15:00
[6] => 13:30:00
[7] => 13:45:00
[8] => 14:00:00
[9] => 14:15:00
[10] => 14:30:00
How to delete the values from above array when my starttime is "12:30:00" and endtime is "14:00:00" ? The values should be deleted are 12:30:00, 12:45:00, 13:15:00, 13:45:00 and 14:00:00
For PHP >= 5.2.0, I would do something like this:
$time_array = array('0' => '11:00:00',
'1' => '12:00:00',
'2' => '13:00:00',
'3' => '14:00:00',
'4' => '15:00:00',
'5' => '16:00:00');
echo "\nBefore:\n";
print_r($time_array);
$start = new DateTime('12:30:00');
$end = new DateTime('14:00:00');
// Magic starts here
foreach ($time_array as $key => $value) {
$datetime = new DateTime($value);
if ($datetime >= $start and $datetime <= $end)
unset($time_array[$key]);
}
// Magic finished
echo "\nAfter:\n";
print_r($time_array);
Yes, it works.
You can use this if you dont need to preserve indexes
error_reporting(E_ALL);
$array = array(
0 => '12:00:00',
1 => '12:15:00',
2 => '12:30:00',
3 => '12:45:00',
5 => '13:15:00',
6 => '13:30:00',
7 => '13:45:00',
8 => '14:00:00',
9 => '14:15:00',
10 => '14:30:00',
);
$from = '12:30:00';
$to = '14:00:00';
$filtered = array();
foreach($array as $key => $value) {
if ($value < $from || $value > $to) {
$filtered[] = $value;
}
}
var_dump($filtered);
$var_arr = array();
$time_arr = array('12:00:00','12:15:00','12:30:00','12:45:00','13:15:00',
'13:30:00','13:45:00','14:00:00','14:15:00','14:30:00');
$start = strtotime('12:30:00');
$end = strtotime('14:00:00');
foreach($time_arr as $arr):
if(strtotime($arr)<$start || strtotime($arr)>$end):
$var_arr[] = $arr;
endif;
endforeach;
print_r($var_arr);