Multidimensional Array Between Dates and Total Price - php

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;

Related

Displaying avg reading in seven days interval

Given an array and I need to to display the average after every seventh row
Array
(
[0] => Array
(
[date] => 01-03-2015
[site_id] => 1
[starting_reading] => 567
[close_reading] => 567
)
[1] => Array
(
[date] => 03-03-2015
[site_id] => 1
[starting_reading] => 567
[close_reading] => 567
)
[2] => Array
(
[date] => 08-03-2015
[site_id] => 1
[starting_reading] => 567
[close_reading] => 567
)
)
Now what i need, is to display all avg reading in 7 days like:
1 to 7
--------------------- avg=close-start----------
8 to 14
--------------------- avg=close-start----------
15 to 21
--------------------- avg=close-start----------
22 to 28
--------------------- avg=close-start----------
29 to 31
--------------------- avg=close-start----------
date starting_reading close_reading
01-03-2015
03-03-2015
--------------------- avg=close-start----------
08-03-2015
--------------------- avg=close-start----------
//$arr is as the array you described
foreach($arr as $k => $v){
//$k will be some integer 0...count($arr)-1 assuming a 'normal' index
//$v is your child array, where you can access attributes such as $v['date']
//every seventh row can be done in a number of ways, the easiest way is:
if($k % 7 == 0){
//then show new line. This will happen every time the number divides into seven. 0, 7, 14, 21... etc.
var_dump($v); will output your child of the seventh (multiple) item
}
}
Including your example for dates and averages assuming array is correctly formatted
//$arr is as the array you described
$close = 0;
$start = 0;
foreach($arr as $k => $v){
//$k will be some integer 0...count($arr)-1 assuming a 'normal' index
//$v is your child array, where you can access attributes such as $v['date']
//every seventh row can be done in a number of ways, the easiest way is:
if($k % 7 == 0){
$close = 0; //reset close
$start = 0; //reset start
}
$close += (int)$v['close_reading'];
$start += (int)$v['starting_reading'];
if(($k + 1) % 7 == 0){ //this is the last row, 6, 13, 20 etc.
echo ($k-5).' to '.($k+1);
echo 'Average: '.(($close - $start) / 7);
}
}

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

Getting week numbers for last X weeks

I have a script that builds an array of week numbers for the last 12 weeks like so:
$week_numbers = range(date('W'), date('W')-11, -1);
However, if the current week number is 1, then this will return an array like so:
Array
(
[0] => 1
[1] => 0
[2] => -1
[3] => -2
[4] => -3
[5] => -4
[6] => -5
[7] => -6
[8] => -7
[9] => -8
[10] => -9
[11] => -10
)
But I need this array to look like this instead:
Array
(
[0] => 1
[1] => 52
[2] => 51
[3] => 50
[4] => 49
[5] => 48
[6] => 47
[7] => 46
[8] => 45
[9] => 44
[10] => 43
[11] => 42
)
Can anyone see a simple solution to this?
I have thought about doing something like this (not tested):
$current_week_number = date('W');
if($current_week_number<12){
// Calculate the first range of week numbers (for current year)
$this_year_week_numbers = range(date('W'), 1, -1);
// Calculate the next range of week numbers (for last year)
$last_year_week_numbers = range(52, 52-(11-$current_week_number), -1);
// Combine the two arrays to return the week numbers for the last 12 weeks
$week_numbers = array_merge($this_year_week_numbers,$last_year_week_numbers);
}else{
// Calculate the week numbers the easy way
$week_numbers = range(date('W'), date('W')-11, -1);
}
one idea
$i = 1;
while ($i <= 11) {
echo date('W', strtotime("-$i week")); //1 week ago
$i++;
}
if you arent scared of loops you can do this:
$week_numbers = range(date('W'), date('W')-11, -1);
foreach($week_numbers as $key => $value) { if($value < 1) $week_numbers[$key] += 52; }
You can do a modulo % trick:
$week_numbers = range(date('W'), date('W')-11, -1);
foreach ($week_numbers as $i => $number) {
$week_numbers[$i] = (($week_numbers[$i] + 52 - 1) % 52) + 1;
}
// -1 +1 is to change the range from 0-51 to 1-52
I've found that using modulo like this is often useful for date calculations, you can something similar for months, using 12.
Well, I think the easiest way is to create array after getting dates:
$week_numbers = array_map(function($iDay)
{
return ($iDay+52)%52?($iDay+52)%52:52;
}, range(date('W'), date('W')-11));
-note, that you can not do just % since 52%52 will be 0 (and you want 52)

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

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]

PHP dates & day counting

I have a scenario where a user submits a start and stop date for a reservation in a hotel. The database has different (daily) prices for periods of the year. Given the user's date range I want to start by counting the number of days the user's range has in each range from the database and then multiplying that by the daily price for that date range.
User's dates:
03 Jun 2012 - 03 Jul 2012
Relevant date ranges from the DB
Array
(
[0] => stdClass Object
(
[date_from] => 2012-04-09
[date_to] => 2012-06-04
[price] => 44
)
[1] => stdClass Object
(
[date_from] => 2012-06-04
[date_to] => 2012-07-02
[price] => 52
)
[2] => stdClass Object
(
[date_from] => 2012-07-02
[date_to] => 2012-07-16
[price] => 61
)
)
As you can see the reservation begins in the first range, spans the second range and ends in the third range.
foreach ($dates as $key => $d)
{
$days = 0;
$user_start = strtotime($range['start']);
$user_stop = strtotime($range['stop']);
$db_start = strtotime($d->date_from);
$db_stop = strtotime($d->date_to);
if( $user_start >= $db_start && $user_stop < $db_stop )
{
$start = $range['start'];
$stop = $range['stop'];
}
elseif( $user_start <= $db_start && $user_stop > $db_start )
{
$start = $d->date_from;
$stop = $d->date_to;
}
elseif( $user_start <= $db_stop && $user_stop > $db_stop )
{
$start = $range['start'];
$stop = $d->date_to;
}
$days = calculate_nbr_days($start, $stop);
$price += $days * $d->price;
}
This code almost works, except for the fact it takes the end of the reservation ($stop) as being the last date of the DB range instead of the user's range and I can't figure out why!
Have you considered doing it all in SQL? Something this should work:
SELECT
SUM(
datediff(
IF($dateTo>date_to,date_to,$dateTo),
IF($dateFrom<date_from,date_from,$dateFrom)
) * price_per_day
) as total_cost
FROM ranges
WHERE '$dateFrom'<=date_to
AND '$dateTo'>=date_from

Categories