基于日期 EDT 和日期的动态文本太平洋夏令时

发布于 2025-01-17 20:55:56 字数 669 浏览 0 评论 0原文

我需要在明天上午 10 点(美国东部夏令时间)和明天上午 10 点(太平洋夏令时间)用 PHP 更改一些按钮文本。

这会起作用和/或有更简洁的方法吗?

$now = time();

$edt = new DateTime("2022-03-30 10:00 EDT");
$pdt = new DateTime("2022-03-30 10:00 PDT");

$edtTS = $edt->getTimestamp();
$pdtTS = $pdt->getTimestamp();

if (in_array($eventId, $edtEvents)) { // this just checks if the event is on the east coast
  if ($now > $edtTS) {
    $btnText = "BUY PRESALE";
  } else {
    $btnText = $status;
  }
}

if (in_array($eventId, $pdtEvents)) { // this just checks if the event is on the west coast
  if ($now > $pdtTS) {
    $btnText = "BUY PRESALE";
  } else {
    $btnText = $status;
  }
}

I need to change some button text with PHP at 10am EDT tomorrow and 10am PDT tomorrow.

Will this work and / or is there a more concise way to do it?

$now = time();

$edt = new DateTime("2022-03-30 10:00 EDT");
$pdt = new DateTime("2022-03-30 10:00 PDT");

$edtTS = $edt->getTimestamp();
$pdtTS = $pdt->getTimestamp();

if (in_array($eventId, $edtEvents)) { // this just checks if the event is on the east coast
  if ($now > $edtTS) {
    $btnText = "BUY PRESALE";
  } else {
    $btnText = $status;
  }
}

if (in_array($eventId, $pdtEvents)) { // this just checks if the event is on the west coast
  if ($now > $pdtTS) {
    $btnText = "BUY PRESALE";
  } else {
    $btnText = $status;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧故 2025-01-24 20:55:56

基于时间戳的比较起作用。 DateTime类型的对象是直接可比的。即使对象具有不同的时区,该比较也可以正常工作。示例:

$edt = new DateTime("2022-03-30 10:00 EDT");  //Eastern Daylight Time
$edtAsUTC = new DateTime("2022-03-30 14:00 UTC");

var_dump($edt == $edtAsUTC, $edt < $edtAsUTC, $edt > $edtAsUTC);
//bool(true) bool(false) bool(false)

尝试一下 https://3v4l.org/zh0je

代替$ now now now now = time = time();现在也可以创建DateTime对象,并用于比较。

$now = new DateTime('now');
//..
if ($now > $edt) {

The comparison based on timestamps works. Objects of type DateTime are directly comparable. The comparison works correctly even if the objects have different time zones. Example:

$edt = new DateTime("2022-03-30 10:00 EDT");  //Eastern Daylight Time
$edtAsUTC = new DateTime("2022-03-30 14:00 UTC");

var_dump($edt == $edtAsUTC, $edt < $edtAsUTC, $edt > $edtAsUTC);
//bool(true) bool(false) bool(false)

Try it on https://3v4l.org/Zh0je

Instead of $now = time(); a DateTime object can also be created for now and used for the comparisons.

$now = new DateTime('now');
//..
if ($now > $edt) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文