skip to Main Content

how can i get Arrival Date and Departure Date from string?

Guest Name :WOLAK, KAMIL – Arrival Date: 2022-09-29 – Departure Date: 2022-10-06 – Ref: H242806

using php

2

Answers


  1. You may use a regular expressions to match the format of yyyy-mm-dd:

    $re = '/dddd-dd-dd/';
    $str = 'WOLAK, KAMIL - Arrival Date: 2022-09-29 - Departure Date: 2022-10-06 - Ref: H242806';
    
    preg_match_all($re, $str, $matches);
    
    // Print the entire match result
    var_dump($matches);
    

    output:

    Array
    (
        [0] => Array
            (
                [0] => 2022-09-29
                [1] => 2022-10-06
            )
    
    )
    
    Login or Signup to reply.
  2. $str = "WOLAK, KAMIL - Arrival Date: 2022-09-29 - Departure Date: 2022-10- 
        06 - Ref: H242806";
    
    
    $data = explode("- ",$str);
    $new_data = [];
    foreach($data as $key => $value){
    
    if(str_contains($value,"Arrival Date:")){
        $date = explode(":", $value);
        $new_data["arrival_date"] = $date[1];
    
    }else if(str_contains($value,"Departure Date:")){
        $date = explode(":", $value);
        $new_data["departure_date"] = $date[1];
    }
    
    }
    
    print_r($new_data);
    

    you will get the result as

    Array ( [arrival_date] => 2022-09-29 [departure_date] => 2022-10-06 )

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search