基于日期 EDT 和日期的动态文本太平洋夏令时
我需要在明天上午 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于时间戳的比较起作用。 DateTime类型的对象是直接可比的。即使对象具有不同的时区,该比较也可以正常工作。示例:
尝试一下 https://3v4l.org/zh0je
代替$ now now now now = time = time();现在也可以创建DateTime对象,并用于比较。
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:
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.