0

I have a string with the value in the format below

10:00 AM, 31 January 2017

The first task my client assigned me was to find January in that string which I have succeeded using the code below

<?php

$season_date = "10:00 AM, 31 January 2017";

if(strpos($season_date, 'January') !== false  ) {
 echo "True"; // Apply price increase percentage
}

?>

Now he has extended the task to find dates range for eg:

20 January - 02 February

How do I go about in doing this?

Edited:

I just realized my problem is bigger than the above. I actually have 2 dates

$start->format('g:i A, d F Y'); and $end->format('g:i A, d F Y');

So it's not just the matter of finding the specified string within the start and end dates, but also in between. Argh.

My final code. Thanks to everyone :)

<?php

    $daterange_season = array('2017-01-20', '2017-02-15'); /* 20 January 2017 - 15 February 2017 : Season */    
    $daterange_booked = array($book_start_date, $book_end_date);

    $range_min = new DateTime(min($daterange_season));
    $range_max = new DateTime(max($daterange_season));

    $start_book = new DateTime(min($daterange_booked));
    $end_book = new DateTime(max($daterange_booked));

    if ($start_book >= $range_min && $end_book <= $range_max) {
      echo 'Yes. Within season. Charge me!';
    } else {
      echo 'My booking is not within Peak Season, dont charge me!';
    }

?>
3
  • @lwani Don't you have year in you second task to get range between two date ? Commented Dec 11, 2016 at 12:38
  • Yes @Tiger I'm required to apply the price increase percentage between 20 January 2017 to 02 February 2017. Commented Dec 11, 2016 at 12:41
  • Guys, thanks for your replies so far. Let me try something first and come back and pick the answer that helped me the most. Thanks again! :) Commented Dec 11, 2016 at 12:49

2 Answers 2

1

Simply use strtotime.

Task 1

$season_date = strtotime("10:00 AM, 31 January 2017");
echo $month=date("F",$season_date);

Task 2

$begin = new DateTime("20 January 2017");
$end = new DateTime("02 February 2017");

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
// $end = $end->modify( '+1 day' );  // Uncomment this line if you want to include end date in the range.

foreach($daterange as $date){
    echo $date->format("Y-m-d") . "<br>";
}

More Details

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @tiger. Let me try this out!
Your answer is very helpful to me. Thank you so much!
0

You're looking for preg_match.

[0-9]{1,2} January

matches any string of type 2 January, 02 January or 20 January.

4 Comments

@Federkun better?
Thanks @TigOldBitties I think I have a bigger problem. I just realized it. I have 2 dates, $start->format('g:i A, d F Y'); and $end->format('g:i A, d F Y'); it's not just the matter of finding the date within the start and end dates, but also in between. Argh.
Regex is incredibly versatile, can help you with strings. Since you already have dates why not work with dates directly? Your question was about strings for which you can always use regex, but in this case try working directly with dates.
Thanks @TigOldBitties let me try this out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.