简体   繁体   English

PHP:如何查找多个strpos()

[英]PHP: How to find multiple strpos()

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'); $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 . 因此,不仅要在start日期和end日期in between找到指定的字符串,而且还要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!';
    }

?>

Simply use strtotime . 只需使用strtotime即可

Task 1 任务1

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

Task 2 任务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 更多细节

You're looking for preg_match . 您正在寻找preg_match

[0-9]{1,2} January

matches any string of type 2 January, 02 January or 20 January. 匹配1月2日,1月2日或1月20日类型的任何字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM