laravel - How to check if new day has started in other timezone in php
Get the solution ↓↓↓I have this code and I want to check if a new day has started in other timezone, let's say in "America/New_York".
$productMarketTime = new DateTime("now", new DateTimeZone('America/New_York'));
$productMarketTime = $productMarketTime->format('h:i a');
$start = DateTime::createFromFormat('h:i a', '12:05 am');
$end = DateTime::createFromFormat('h:i a', '06:30 am');
dd([
'now' => $productMarketTime,
'start' => $start,
'end' => $end,
'condition' => $productMarketTime > $start && $productMarketTime < $end
]);
but it is not working correctly, I want this condition to be true when the time is between12:05 am
and06:30 am
.
What am I doing wrong here?
Answer
Solution:
You have to use the date objects in the comparison... Not the strings returned by.format()
. Also, make sure to use the same timezone on every dates to compare.
<?php
$newyork_tz = new DateTimeZone('America/New_York');
$now_newyork = new DateTime('now', $newyork_tz);
$start = new DateTime('12:05 am', $newyork_tz);
$end = new DateTime('06:30 am', $newyork_tz);
$condition = $now_newyork > $start && $now_newyork < $end; // true/false
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: git was not found in your path, skipping source download
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.