【问题标题】:I want the result of the array php to be replaced with the current time我希望将数组 php 的结果替换为当前时间
【发布时间】:2019-05-04 09:04:54
【问题描述】:

我有一个包含时间和价格以及时间的数组,我想检查价格对应的时间数组:

 $myArray = array('8:00am'=>'200usd', '13:00pm'=>'300usd');
 $timecurrent = date("H:ia");

我想要结果:时间 8:00am -> 12:59pm 设定价格 200usd

【问题讨论】:

标签: php arrays datetime


【解决方案1】:

检查我的解决方案

$myArray = array('8:00am'=>'200usd', '9:00am'=>'250usd', '13:00pm'=>'300usd');
//Get current time
$timecurrent = DateTime::createFromFormat('H:ia', date('H:ia'));
$price = null;
$actualPrice = null;
foreach($myArray as $time=>$fee){
    $date = DateTime::createFromFormat('H:ia', $time);
    if($timecurrent<$date){ //if crossed current time
        $actualPrice = $price; //Get previous element time and stop
        break;
    }
    //Save fee for next iteration
    $price = $fee;
}

echo $actualPrice; //250usd

【讨论】:

  • 谢谢!我已经调整了
猜你喜欢
  • 1970-01-01
  • 2015-02-14
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
相关资源
最近更新 更多