I have a wordpress loop and need to create an array of the hours within each day, this works fine as long as the start and end do not span multiple days. Eg: 11pm to 7am the following day. I would like to create a new array item for each 24 hour period. SO if a date spans from one day to the next I would rather have multiple dates.
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$meta = get_post_meta($the_query->post->ID);
$start = date('G',strtotime($meta['start'][0]));
$end = date('G',strtotime($meta['end'][0]));
$arr[date('Y/m/d',strtotime(get_the_date()))] = array(
'start' => $start,
'end' => $end
);
}
}
array looks like this,
Array
(
[2021/10/28] => Array
(
[start] => 0
[end] => 8
)
[2021/10/27] => Array
(
[start] => 0
[end] => 8
)
[2021/10/25] => Array
(
[start] => 0
[end] => 8
)
[2021/10/22] => Array
(
[start] => 0
[end] => 8
)
[2021/10/21] => Array
(
[start] => 0
[end] => 8
)
[2021/10/20] => Array
(
[start] => 0
[end] => 8
)
[2021/10/19] => Array
(
[start] => 0
[end] => 8
)
)
Solution - 1
can you share the desired output of the array when date date spans from one day to the next ?
Solution - 2
To get the desired array, firstly, we need to arrive at the difference between the start and end dates. And then, loop over the number of days between start and end dates with few conditions that fits our requirement and finally feed them into the array.
Here is a code you can try:
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$meta = get_post_meta($the_query->post->ID);
$start = date('G',strtotime($meta['start'][0]));
$end = date('G',strtotime($meta['end'][0]));
$start_datetime = strtotime($meta['start'][0]);
$end_datetime = strtotime($meta['end'][0]);
$start_date = date('Y/m/d',$start_datetime);
$end_date = date('Y/m/d',$end_datetime);
// Calculating the difference in timestamps
$diff = $end_datetime - $start_datetime;
// 1 day = 24 hours
// 24 * 60 * 60 = 86400 seconds
$daysbetween = (abs(round($diff / 86400)) +1);
// echo $daysbetween;
for ($i = 1; $i <= $daysbetween; $i++) {
if (($i == 1 ) and ($i == $daysbetween)) {
// single date
$datekey = $start_date;
$startval = $start;
$endval = $end;
}
if (($i == 1 ) and ($i != $daysbetween)) {
// more days exist, this is first date
$datekey = $start_date;
$startval = $start;
$endval = '23';
}
if (($i > 1) and ($i < $daysbetween)) {
// days in between
$datekey = date('Y/m/d', strtotime($start_date .'+' .($i-1) .' days'));
$startval = '0';
$endval = '23';
}
if (($i > 1) and ($i == $daysbetween)) {
// the last date
$datekey = $end_date;
$startval = '0';
$endval = $end;
}
//push the item into your array
$arr[$datekey] = array(
'start' => $startval,
'end' => $endval
);
}
}
}
your array will be in $arr. You can print it or use it further in your application. The output will be in this format that you expected:
Array
(
[2014/08/12] => Array
(
[start] => 9
[end] => 23
)
[2014/08/13] => Array
(
[start] => 0
[end] => 11
)
)
Here is a link where you can view the output of this code (slightly modified to remove wordpress functions):
https://paiza.io/projects/e/Oz6G6M28DUjOM0Mf1ozccA
|