PHP, multidimensional arrays: to intersected events with days of the month - php

I want to build a movie timetable. I have $array1 containing the movie titles and airing dates and times:
array =
0: array =
Title: string = American Beauty
Date: string = 25/09/2012
Time: string = 15:00 - 16:20
1: array =
Title: string = The Godfather
Date: string = 25/09/2012
Time: string = 16:20 - 18:20
2: array =
Title: string = Pulp Fiction
Date: string = 26/09/2012
Time: string = 15:00 - 16:20
And I have $array2 containing the days of the month grouped by Mondays, Tuesday s, Wednesday s, Thursday s and Fridays (no movies during the weekend)
Array
(
[1] => Array
(
[0] => 3
[1] => 10
[2] => 17
[3] => 24
[4] =>
)
[2] => Array
(
[0] => 4
[1] => 11
[2] => 18
[3] => 25
[4] =>
)
[3] => Array
(
[0] => 5
[1] => 12
[2] => 19
[3] => 26
[4] =>
)
[4] => Array
(
[0] => 6
[1] => 13
[2] => 20
[3] => 27
[4] =>
)
[5] => Array
(
[0] => 7
[1] => 14
[2] => 21
[3] => 28
[4] =>
)
)
I need to intersect these two arrays so I can print under day 25 the movie “American Beauty” also under day 25 “The Godfather” and under day 26 “Pulp Fiction”.
Meaning I need to print:
SEPTEMBER 2012
Monday Tuesday Wednesday ....
3 4 5
10 11 12
17 18 19
24 25 26
15:00-16:20 American Beauty 15:00-16:20 Pulp Fiction
16:20-18:20 The Godfather
My tries so far:
foreach( $array1 as $key => $value )
{
$theTime = $value['Time'];
$theTime = explode("/", $theTime );
$days[] = $theTime [0];
$months[] = $theTime [1];
}
So I have all the airing days in array $days but from here I don’t know how to follow or even if this approach is the correct one.
Here's how I get $array2:
$month = 9;
$year = 2012;
for ($i = 1; $i <= 31; $i++)
{
$timestamp = mktime(0, 0, 0, $month , $i, $year);
if (date("n", $timestamp) == $month )
{
$day = date("N", $timestamp);
// Monday 1 to Friday 5
if ($day == 1 OR $day <= 5) {
$array2[$day][] = date("j", $timestamp);
}
}
}
Please help, I’m stuck.
Thanks very much

Ok you will iterate over $array1 and parse the date of the movie.
Then, you will look inside array2 if your day is there
foreach($array1 as $movie){
$movieDate = new DateTime($movie);
//as you indexed $array2 with monday = 1 -> friday = 5 you can use the "w" format
if(isset($array2[$movieDate->format("w"))){
$array[$movieDate->format("j")][] = $movie;
}
}
I made some mutation in your output array, it becomes :
SEPTEMBER 2012
Monday Tuesday Wednesday ....
3=>[] 4 =>[] 5=>[]
10=>[] 11 =>[] 12=>[]
17=>[] 18 =>[] 19=>[]
24 =>[] 25=>[ 26 =>[
15:00-16:20 American Beauty, 15:00-16:20 Pulp Fiction]
16:20-18:20 The Godfather]

Related

array_merge seems not to be recognised key. I would like to insert numeric key but it always starts from 0

I would like to make an array like below.
The key should be day. The value should be string weekday name.
$array = [
1 =>['mon'],
2 =>['tue'],
3 =>['wed']
];
However $day (key) can't seem to be recognised.
CODE is
//$i is the first day of a specific month. this sample is 2022-11-01
for($j = $i ; (int)date_create($j)->format('w') < 6 ; $j++){
print_r("くj");
print_r($j);
$day = date_create($j)->format('j');
print_r("day");
print_r($day);
$dailyArray = array_merge($dailyArray ,array( $day => mb_strtolower(date_create($j)->format('D'))));//
print_r("THE RESULT!!");
print_r($dailyArray);
result(November/2022) is
THE RESULT!! day4
Array
(
[0] => Tue
[1] => Wed
[2] => Thu
[3] => Fri
)
It's not the question, but it can't set (int)date_create($j)->format('w') <= 6 . How can I do the loop until sat?
The following script was based on your code basically and it will print out the result as expected, till Saturday
$dailyArray = array();
$d = 1;
while($d < 6 ){
$j = implode("-", array(2022, 11, str_pad($d, 2, '0', STR_PAD_LEFT)));
print_r("くj");
print_r($j);
$day = date_create($j)->format('j');
print_r("day");
print_r($day);
$dailyArray = array_merge($dailyArray , array( $day => strtolower(date_create($j)->format('D'))));
$d++;
}
print_r("<p>THE RESULT!!");
print_r($dailyArray);
It should print out
THE RESULT!!Array ( [0] => tue [1] => wed [2] => thu [3] => fri [4] => sat )
Actually, if (int)date_create($j)->format('w') < 6 is preferred, a shorter version could be
$d = 1;
$j = implode("-", array(2022, 11, str_pad($d, 2, '0', STR_PAD_LEFT)));
while((int)date_create($j)->format('w') < 6 ){
$j = implode("-", array(2022, 11, str_pad($d, 2, '0', STR_PAD_LEFT)));
$dailyArray[$d] = strtolower(date_create($j)->format('D'));
$d++;
}
It printed out:
THE RESULT!!Array ( [1] => tue [2] => wed [3] => thu [4] => fri [5] => sat )

output days of the week via function

help me please
I have a code
<?php
// date on Russian
function getDateRus(){
$monthes = array(
1 => 'Января', 2 => 'Февраля', 3 => 'Марта', 4 => 'Апреля',
5 => 'Мая', 6 => 'Июня', 7 => 'Июля', 8 => 'Августа',
9 => 'Сентября', 10 => 'Октября', 11 => 'Ноября', 12 => 'Декабря'
);
return ( (int)date('d') . ' ' . $monthes[(date('n'))] . date(' Y'));
}
// day on Russian
function getDayRus(){
$days = array(
'Воскресенье', 'Понедельник', 'Вторник', 'Среда',
'Четверг', 'Пятница', 'Суббота'
);
return $days[(date('w'))];
}
echo "Today:" . getDateRus() . ", " . getDayRus();
?>
I need to put days off for a week ahead:
25 September, Monday
26 September, Tuesday
27 September, Wednesday
28 September, Thursday
29 September, Friday
30 September, Saturday
01 October, Sunday
I can print the dates for the week ahead, but without the Russian language.
how to deduce the dates for the week ahead and save the Russian language?
I changed your structure slightly and I made use of the powerful function DateTime
$monthes = array(
1 => 'Января', 2 => 'Февраля', 3 => 'Марта', 4 => 'Апреля',
5 => 'Мая', 6 => 'Июня', 7 => 'Июля', 8 => 'Августа',
9 => 'Сентября', 10 => 'Октября', 11 => 'Ноября', 12 => 'Декабря'
);
$days = array(
'Воскресенье', 'Понедельник', 'Вторник', 'Среда',
'Четверг', 'Пятница', 'Суббота'
);
$dArray = array();
$date = new DateTime();
for($i=0; $i<7;$i++){
$dArray[] = $date->format("d").' '.$monthes[$date->format("n")].','.$days[$date->format("w")];
$date->modify("+1 day");
}
print_r($dArray);
Output:
Array ( [0] => 25 Сентября,Понедельник [1] => 26 Сентября,Вторник [2] => 27 Сентября,Среда [3] => 28 Сентября,Четверг [4] => 29 Сентября,Пятница [5] => 30 Сентября,Суббота [6] => 01 Октября,Воскресенье )
You can probs achieve this better by using strftime and setlocale. That way you will not need your day/month arrays

PHP: print all days between a given day and end of month

I don't want to return DATE (Y-m-d).
I need to print all days until end of month from a given day independently of the month or year.
I tried both [$array as $i] - [$array as $key] and didn't work.
$myday (for example = 19)
return $days
would result:
Array
[0] => 20
[1] => 21
[2] => 22
[3] => 23
...
[31] => 31 || [30] => 30 || [28] => 28
I would need each value for $days to compare each to another field.
Didn't try to use $myday as regular number instead of treating as date. And not use strtotime, mktime....
EDITING
Need something very simple like this one:
$output = array();
for ($i=$myday+1;$i<=31 || $i<=30 || $i<=28;$i++) {
$output[] = $i;
}
But print_r won't do it, I need to return as each value to use in different if conditions
This is easily done using DateTime(), DateInterval(), DatePeriod(), and relative date formats.
$start = (new DateTime())->setDate(date('Y'), date('m'), $myday + 1);
$end = new DateTime('first day of next month');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
$days = array();
foreach($period as $date) {
$days[] = $date->format('d');
}
Results
Array
(
[0] => 20
[1] => 21
[2] => 22
[3] => 23
[4] => 24
[5] => 25
[6] => 26
[7] => 27
[8] => 28
[9] => 29
[10] => 30
[11] => 31
)
Demo

Multidimensional Array Between Dates and Total Price

I'm trying to make PHP array and here is my example.
In each array first row is unique record number "[767962]"
After inside that array hotel room id 1st row.
2nd is hotel name
3rd and 4th are first price range between
5th is the price
Array
(
[767962] => Array /// --secial id for price record--//
(
[0] => 42923 /// --hotelroomid--//
[1] => Crystal Hotels Kemer Deluxe Resort /// --Hotel Name--//
[2] => 2013-04-01 /// --Start date--//
[3] => 2013-05-16 /// --End Date--//
[4] => 179 /// --Price --//
)
[767964] => Array
(
[0] => 42923
[1] => Crystal Hotels Kemer Deluxe Resort
[2] => 2013-05-17
[3] => 2013-05-26
[4] => 239
)
[767980] => Array
(
[0] => 42940
[1] => Rixos Deluxe Resort
[2] => 2013-03-02
[3] => 2013-05-26
[4] => 340
)
)
Here is the expected output.
I would like to know total price between that hotel room 2013-05-14 - 2013-05-21
1 , 42923 , Crystal Hotels Kemer Deluxe Resort , 2013-05-14 , 2013-05-21 , 1553
2 , 42940 , Rixos Deluxe Resort , 2013-05-14 , 2013-05-21 , 2380
For example 1553 is the total price between 2013-05-14 - 2013-05-21 dates.
2013-05-14 - 179
2013-05-15 - 179
2013-05-16 - 239
2013-05-17 - 239
2013-05-18 - 239
2013-05-19 - 239
2013-05-20 - 239
2013-05-21 - 239
Total is 1553
Bro i did this kind of work in my application i think this will help you mostly but if something going wrong please let me know i will change.......
For example please refer this link for Demo;
$room_id = 42923;
$start_date = "2013-05-10 00:00:00";
$end_date = "2013-05-21 00:00:00";
find dates between above two dates
$day = 86400; // Day in seconds
$format = 'Y-m-d'; // Output format (see PHP date funciton)
$sTime = strtotime($start_date); // Start as time
$eTime = strtotime($end_date); // End as time
$numDays = round(($eTime - $sTime) / $day) + 1;
$days = array();
for ($d = 0; $d < $numDays; $d++) {
$days[] = date($format, ($sTime + ($d * $day)));
}
Now for each date you need to check in which array it falls
foreach($days as $key => $d)
{
foreach($data as $key1 => $dat)
{
if($dat[0] == $room_id) {
if($d >= $dat[2] && $d<= $dat[3]) {
$amount += $dat[4];
}
}
}
}
echo $amount;

Year value error from PHP date() [duplicate]

This question already has answers here:
Display all the week numbers between two dates in PHP [duplicate]
(3 answers)
Closed 8 years ago.
Given a start and end date, I need to generate an array with year and week values.
For example:
Start Date: 2013-01-01
End Date: 2013-02-23
Generated Array:
Array ( [0] => Array ( [week] => 01 [year] => 2013 )
[1] => Array ( [week] => 02 [year] => 2013 )
[2] => Array ( [week] => 03 [year] => 2013 )
[3] => Array ( [week] => 04 [year] => 2013 )
[4] => Array ( [week] => 05 [year] => 2013 )
[5] => Array ( [week] => 06 [year] => 2013 )
[6] => Array ( [week] => 07 [year] => 2013 )
[7] => Array ( [week] => 08 [year] => 2013 ) )
Here is the code I'm using to generate this:
public static function getYearWeekRange($startdate, $enddate) {
$array = array();
$starttime = strtotime($startdate);
$endtime = strtotime($enddate);
while ($starttime <= $endtime) {
$year = date('Y', $starttime);
$week = date('W', $starttime);
$array[] = array('week' => $week, 'year' => $year);
$starttime = strtotime('+1 week', $starttime);
}
return $array;
}
My problem is that when I generate certain date ranges, I don't get the correct year value at the start of the 2013 year. For example:
Start Date: 2012-01-01
End Date: 2013-02-23
In this case, where it should have an subarray with year = 2013 and week = 01, it actually has it's year value equal to 2012.
If I were to switch the start date to 2013-01-05 for example, then there is no problem.
Can anyone offer a solution that would guarantee that my year and week values are always correct?
I was able to fix my problem using the following:
public static function getWeekRange($startdate, $enddate) {
$array = array();
$p = new DatePeriod(
new DateTime($startdate),
new DateInterval('P1W'),
new DateTime($enddate)
);
foreach ($p as $w) {
$array[] = array('year' => $w->format('o'), 'week' => $w->format('W'));
}
return $array;
}

Categories