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?

3
  • @DainisAbols Yes, I changed it. type of all of three arguments are object (date time) Commented Oct 24, 2017 at 11:02
  • The code is not relevant to the question. " changing date's year to current year" has nothing to do with date comparison. Commented Oct 24, 2017 at 11:03
  • please describe getTimeStartNoon & getTimeEndNoon methods Commented Oct 25, 2017 at 7:14

2 Answers 2

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; } 
Sign up to request clarification or add additional context in comments.

4 Comments

what the relevant of your code to change year??
First convert time then use it in if statement
He wants just check in times!
i dont understand he use $foodTime->getTimeStartNoon() but no describe getTimeStartNoon method , how your code fix getTimeStartNoon problem because this method return wrong year
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())); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.