0

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

  • Have you tried something? It's pretty straight forward (though a little long) with `DateTime` related classes. Even if you're using PHP<5.3, `strtotime` is still doable. – Passerby Mar 08 '13 at 10:27
  • You should use a database for your data and proper SQL queries to extract informations. – Ghigo Mar 08 '13 at 10:31
  • Also, what about the "Rixos" room? It doesn't start until 17th, but you need to start your trip at 14th, where are you going to stay before 17th, and how does that price come from? – Passerby Mar 08 '13 at 10:35
  • this is the result it comes from SQL database and i converted to array. the only soution is using and array to find the result. i can ask each date for daily price but it makes huge ammount of quary and load to the server. – Birol Duman Mar 08 '13 at 10:36
  • i just fixed it,it is an example actually i wrote it now for an example my mistake sorry :) – Birol Duman Mar 08 '13 at 10:39
  • I still don't think that you need arrays. SQL will do the job for you. – Ghigo Mar 08 '13 at 10:40
  • in some cases it is very useful to use SQL but this is the only list of result i get from server. i need a way to calculate with in an array – Birol Duman Mar 08 '13 at 10:44

1 Answers1

0

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;
Venkata Krishna
  • 4,287
  • 6
  • 30
  • 53
  • thank you :) its geting closer but actually the code has a problem i guess but it gives me an idea thank you – Birol Duman Mar 08 '13 at 16:44
  • Mann your great !! thank you very much:) that the best code and help i got in my life belive me. i called most of my friends no body helped me but you did :) thank again.. have a lovely dayy – Birol Duman Mar 11 '13 at 10:22