Reputation: 303
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!';
}
?>
Upvotes: 0
Views: 187
Reputation: 2907
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>";
}
Upvotes: 1
Reputation: 1337
You're looking for preg_match.
[0-9]{1,2} January
matches any string of type 2 January, 02 January or 20 January.
Upvotes: 0