0

I have a couple of date times :

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"
}

I just demand to determine the current time is between first and second times. Here is my code:

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);

    }

The essential issue I should fix is changing date's year to current year. is it possible with Carbon (or pure PHP) and how?

Farzan Najipour
  • 2,442
  • 7
  • 42
  • 81

2 Answers2

0

you can use this method

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

}
Honarkhah
  • 553
  • 7
  • 19
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;
}
FarhadKh
  • 49
  • 3