-4

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

mohammed
  • 17
  • 5
  • [how do i extract date from string using php](https://stackoverflow.com/q/44360982/2943403) and [Extract dates from a string in php](https://stackoverflow.com/q/24288340/2943403) and [regex to get date yyyy-mm-dd from any string](https://stackoverflow.com/q/19564063/2943403) and [regular expression for extracting multiple dates](https://stackoverflow.com/q/40504259/2943403) – mickmackusa Nov 03 '22 at 01:04
  • [PHP preg_match yyyy-mm-dd](https://stackoverflow.com/q/21064892/2943403) and [PHP regex expression to find if a string contains YYYY-MM-DD](https://stackoverflow.com/q/10476134/2943403) So, you see, there is no reason why you should be at a complete loss for making a coding attempt. Regex isn't necessarily a requirement either. https://3v4l.org/KTceu – mickmackusa Nov 03 '22 at 01:32

2 Answers2

0

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

$re = '/\d\d\d\d-\d\d-\d\d/';
$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
        )

)
IT goldman
  • 14,885
  • 2
  • 14
  • 28
0
$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 )

Shooter
  • 51
  • 6