如何检查 php 的其他时区是否开始了新的一天

How to check if new day has started in other timezone in php

我有这段代码,我想检查新的一天是否在其他时区开始,比如说在“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
]);

但是它不能正常工作,我希望这个条件在时间介于 12:05 am06:30 am 之间时为真。

我做错了什么?

您必须在比较中使用日期对象...而不是 .format() 返回的字符串。另外,请确保在每个日期上使用相同的时区进行比较。

<?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
?>

Demo on paiza.io