I have a multidimensional array like this:
$array = array(
0 => array(
'name' => 'first element',
'start' => '30/04/2015',
'end' => '30/06/2015'
),
1 => array(
'name' => 'second element',
'start' => '01/07/2015',
'end' => '30/09/2015'
),
2 => array(
'name' => 'fourth element',
'start' => '01/10/2015',
'end' => '15/12/2015'
)
);
I need to select one array subarray (item) based on the today date.
today date must be between start date and end date keys.
In the end I would like to have this:
$selected_subarray = array (
'name' => 'first element',
'start' => '30/04/2015',
'end' => '30/06/2015'
)
I use to check between two dates like this:
function check_if_in_range($start_date, $end_date, $today_date)
{
$start_d = strtotime($start_date);
$end_d = strtotime($end_date);
$today_d = strtotime($today_date);
return (($today_d >= $start_d) && ($today_d <= $end_d));
}
I tryed to follow suggestions from this question How to search by key=>value in a multidimensional array in PHP
but if I'm able to filter for a key = value, I'm not able to do the same using the "check_if_in_range" function.
You do know that 30/06/2015 is invalid date, and strtotime() will return false? See here. Format mm/dd/yyyy is an American month, day and year. So your format is non-standard.
Best way is to convert it to standard format, and then use strtotime()example or just use DateTime::createFromFormat()example.
After you learn how formating and converting dates works, then you can just do simple foreach loop, and break on first found result. Here is a little demo.
Try something like the following
foreach($array as $key => $value) {
if(check_in_range($value['start'], $value['end'], $today_date)) {
$selected_subarray = $value;
}
}
Related
Im not great at PHP but I typed up the following code to change the header image based on the date array I have set.
Im wondering if the code looks alright and will function as intended or if iv missed something in my coding.
$hollidayevents = array(
array(
'image' => 'wp-content/uploads/1.png',
'start' => '01-02',
'end' => '02-02'
),
array(
'image' => 'wp-content/uploads/2.png',
'start' => '03-02',
'end' => '04-02'
)
);
foreach($hollidayevents as $myevent) {
if(date('d-m') >= $myevent['start'] && date('d-m') <= $myevent['end']) {
echo "<img src='".$myevent['image']."'>";
}
else { echo "<img src='wp-content/uploads/default header image'>";}
}
When you compare the dates as string with 'd-m' format the result is based on the day and only if days are same then it comes to compare months too.
For example imagine this: '20-10' > '01-11', both are with format you used 'd-m' what is the result ? First is higher then second so the result is true, even tho you would expect the opposite. Thats because only first character gets compared.
$hollidayevents = array(
array(
'image' => 'wp-content/uploads/1.png',
'start' => '02-01', // <- m-d format
'end' => '02-02' // <- m-d format
),
array(
'image' => 'wp-content/uploads/2.png',
'start' => '02-03', // <- m-d format
'end' => '02-04' // <- m-d format
)
);
$eventFound = false;
foreach($hollidayevents as $myevent) {
if(date('m-d') >= $myevent['start'] && date('m-d') <= $myevent['end']) {
echo "<img src='".$myevent['image']."'>";
$eventFound = true;
break;
}
}
if(!$eventFound){
echo "<img src='wp-content/uploads/default header image'>";
}
I want to display the start date and end date of the week. I have One date and a string like 1W4 and,in 1W4 consider 4 weeks and 1 visit so, my string like this 2W4,1W2,3W3,1W1,2W4.
I want to make start date and end date of week array according to string and week start from Sunday to Saturday.
Please post me if anyone has solution.Please ignoring if mistake in asking Question.
Thank you.
Try my php code:
From php.net datetime.format:
W: ISO-8601 week number of year, weeks starting on Monday.
The first calendar week of a year is that one which includes the first Thursday of that year.
So I have to rest one day to the start week date.
I assumed that the weeks correspond to the current year.
Input string:
$weeksString = "2W4,1W2,3W3,1W1,2W4";
Code:
<?php
$weeksArray = explode(",", $weeksString);
$result = array();
foreach($weeksArray as $visitsWeek) {
list($visits, $week) = explode("W", $visitsWeek);
$startDate = date("Y-m-d", strtotime(date("Y") . "W" . str_pad($week, 2, "0", STR_PAD_LEFT) . " -1 days"));
$endDate = date("Y-m-d", strtotime($startDate . " +6 days"));
$result[] = array("week" => $week, "startDate" => $startDate, "endDate" => $endDate);
}
?>
Output array $result:
array ( 0 => array ( 'week' => '4', 'startDate' => '2021-01-24', 'endDate' => '2021-01-30', ), 1 => array ( 'week' => '2', 'startDate' => '2021-01-10', 'endDate' => '2021-01-16', ), 2 => array ( 'week' => '3', 'startDate' => '2021-01-17', 'endDate' => '2021-01-23', ), 3 => array ( 'week' => '1', 'startDate' => '2021-01-03', 'endDate' => '2021-01-09', ), 4 => array ( 'week' => '4', 'startDate' => '2021-01-24', 'endDate' => '2021-01-30', ), )
I need to do a query and get certain kind of data. I have 2 tables, users and connections, I need to get per user how many times he/she connected per month and year.
users connections
........... ................
john 10/02/2014
john 15/02/2014
john 03/01/2015
john 06/02/2015
Is there a chance to get this info in this format:
john=>
[0]=>2014
[0]=>02
'total' =>2
[1]=>2015
[0]=>01
'total' => 1
[1]=>02
'total' => 2
[2]=>03
'total'=> 1
I'm using Codeigniter and also PHP.
Answering to #CodeGodie what I've done so far is:
public function getPeriodicity(){
$this->db->select('u.vusr_user, extract (MONTH from (to_timestamp(c.vuc_log_in))) as month, extract (YEAR from (to_timestamp(c.vuc_log_in))) as yearly, COUNT(c.vuc_log_in)');
$this->db->from('vts_users_conn c');
$this->db->join('vts_users u', 'c.vuc_vusr_id = u.vusr_id');
$this->db->group_by('u.vusr_user, month, yearly','asc');
$query = $this->db->get();
return $query->result_array();
}
Assuming you are using Codeigniter's $this->db->result_array() to obtain your database results, your initial array will look like this:
$res = array(
array(
"name" => "john",
"date" => "10/02/2014"
),
array(
"name" => "john",
"date" => "15/02/2014"
),
array(
"name" => "john",
"date" => "03/01/2015"
),
array(
"name" => "john",
"date" => "06/02/2015"
),
array(
"name" => "john",
"date" => "06/03/2015"
)
);
In order to change this array to your desired output, I would do the following:
foreach ($res as $row) {
$date_arr = explode("/", $row['date']);
$n = $row['name'];
$y = $date_arr[2];
$m = $date_arr[1];
if (!isset($final[$n]))
$final[$n] = array();
if (!isset($final[$n][$y]))
$final[$n][$y] = array();
if (!isset($final[$n][$y][$m])) {
$final[$n][$y][$m] = array("total" => 1);
} else {
$final[$n][$y][$m]["total"] = $final[$n][$y][$m]["total"] + 1;
}
}
If you var_dump your final result (var_dump($final)), you will get the following:
array (size=1)
'john' =>
array (size=2)
2014 =>
array (size=1)
'02' =>
array (size=1)
'total' => int 2
2015 =>
array (size=3)
'01' =>
array (size=1)
'total' => int 1
'02' =>
array (size=1)
'total' => int 1
'03' =>
array (size=1)
'total' => int 1
Hope this helps.
As a general rule, if you can access the data and see in your mind how you want that data to look, then it's pretty much possible to get it to do that. It's just a matter of working out the process.
In your case, I would do the following steps:
Order the data by users, then by date so everything is nicely together
Loop through the data and each time, check that the current user is the same as the last one. if it's not, create a new array key
split the date into the parts you want
check the user array for the key relating to year for that user. If the year exists, search for the month. If the month exists, add 1 to the total for that month. If the year and/or month don't exist, create the keys and set the total to be 1 for that month
Once the records have been processed, you should have the data in the format you need.
I'm trying to let users post from the front end into a nested ACF repeater field.
I've got the sub fields in the first repeater posting fine. But, I can't quite figure out how to get the nested repeater working properly.
Here's the code I have so far:
$event_field_key = 'field_535e6b9ffe3da';
$events[] = array(
'start-date' => $startDate,
'end-date' => $endDate,
'notes' => $_POST['p'.$p.'-notes'],
'start-end-times' => array(
'start-time' => '09:00', // would be dynamic
'end-time' => '17:00' // would be dynamic
)
);
update_field($event_field_key, $events, $post_id);
I'm not sure if I can just nest another array in there, or if I need to do something else.
[UPDATE]
I've just done this and it does input into the first row:
$event_field_key = 'field_535e6b9ffe3da';
$events[] = array(
'start-date' => $startDate,
'end-date' => $endDate,
'notes' => $_POST['p'.$p.'-notes'],
'start-end-times' => array(
'start-time' => '9:00',
'end-time' => ' 17:00'
)
);
update_field($event_field_key, $events, $post_id);
However, this code puts row 1 values both as 9 and row 2 values as 1.
So it looks like:
Row 1: start time: 9, end time: 9
Row 2: start time: 1, end time: 1
I can't seem to find any documentation on this, but it looks like it's possible, just a case of figuring out the syntax.
The fix was an array of arrays:
$event_field_key = 'field_535e6b9ffe3da';
$events[] = array(
'start-date' => $startDate,
'end-date' => $endDate,
'notes' => $_POST['p'.$p.'-notes'],
'start-end-times' => array(
array(
'start-time' => '09:00',
'end-time' => '17:00'
),
array(
'start-time' => '10:00',
'end-time' => '16:00'
)
)
);
update_field($event_field_key, $events, $post_id);
I am trying to sort an array in PHP by date and time which is in ISO 8601 format. I am still trying to grasp PHP and have tried many of the solutions on stack overflow and I am just not able to nail down the right function. Hopefully this is an easy answer and it will be helpful to others.
FYI, this array was generated by the Citrix API for GoToMeeting. I would like to sort the array based on startTime in the soonest time first in the list.
Here is what the array looks like using var_export with two results presented:
array (
0 => stdClass::__set_state(
array(
'createTime' => '2012-07-03T19:36:58.+0000',
'status' => 'INACTIVE',
'subject' => 'Client 1',
'startTime' => '2012-07-10T14:00:00.+0000',
'conferenceCallInfo' => 'United States: xxxxx Access Code: xxxxx',
'passwordRequired' => 'false',
'meetingType' => 'Scheduled',
'maxParticipants' => 26,
'endTime' => '2012-07-10T15:00:00.+0000',
'uniqueMeetingId' => 12345678,
'meetingid' => 123456789,
)
),
1 => stdClass::__set_state(
array(
'createTime' => '2012-07-02T21:57:48.+0000',
'status' => 'INACTIVE',
'subject' => 'Client 2',
'startTime' => '2012-07-06T19:00:00.+0000',
'conferenceCallInfo' => 'United States: xxxxx Access Code: xxxxx',
'passwordRequired' => 'false',
'meetingType' => 'Scheduled',
'maxParticipants' => 26,
'endTime' => '2012-07-06T20:00:00.+0000',
'uniqueMeetingId' => 12345678,
'meetingid' => 123456789,
)
),
)
My goal is to then output the array into html div's using a foreach loop, this code is complete and works well but my sort is off :-)
Thank you in advance for any help!
Steve
You can implement any sorting technique you can think of if you wrap it in a callback and use usort() docs here
inside your callback, you can use strtotime or similar, and do simple int comparisons.
$myDateSort = function($obj1, $obj2) {
$date1 = strtotime($obj1->startTime);
$date2 = strtotime($obj2->startTime);
return $date1 - $date2; // if date1 is earlier, this will be negative
}
usort($myArray, $myDateSort);