PHP - 将日期改为当年

时间:2017-10-24 10:55:09

标签: php datetime php-carbon

我有几个约会时间:

DateTime {#2205 ▼
  +"date": "1970-01-01 12:30:00.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}
DateTime {#2206 ▼
  +"date": "1970-01-01 20:00:00.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}

我只是要求确定当前时间是第一次和第二次之间。这是我的代码:

private function dateIsBetween($from, $to,$currentdate) {
    if ($currentdate >= $from && $currentdate <= $to) {
        return true;
    }
    return false;
}

public function checkTimes($foodTime) {
         $currentTime = new \DateTime();
        var_dump($foodTime->getTimeStartNoon(),$foodTime->getTimeEndNoon());
        return $this->dateIsBetween($foodTime->getTimeStartNoon(),$foodTime->getTimeEndNoon(),$currentTime);

    }

我应该解决的基本问题是将日期改为当年。是否有可能使用Carbon(或纯PHP)以及如何使用?

2 个答案:

答案 0 :(得分:0)

你可以使用这种方法

public function checkTimes($foodTime) {
    $currentTime = Carbon::now();
    var_dump($foodTime->getTimeStartNoon(),$foodTime->getTimeEndNoon());
    return ($currentTime->gt($foodTime->getTimeStartNoon()) && $currentTime->lt($foodTime->getTimeEndNoon()));

}

答案 1 :(得分:0)

public function convertRestaurantTime($time): string
{
    if (gettype($time) == 'string') {
        $time = new \DateTime($time);
    }

    $time   = Carbon::createFromFormat('H:i:s', Carbon::instance($time)->format('H:i:s'), 'Asia/Tehran');
    $hour   = $time->format('G');
    $minute = $time->format('i');

    if ($minute != '00') {
        return $hour . '.' . $minute;
    }

    return $hour;
}