php - 从具有特定时区和特定 $now 值的相对日期/时间字符串获取时间戳

标签 php datetime timezone strtotime

我知道在 PHP 中从相对日期/时间字符串获取时间戳的两种方法:

strtotime:允许用户指定自己的 $now 值

DateTime 对象:允许用户指定他们自己的 $timezone 值

有没有一种方法可以从允许指定时区和 $now 值的日期/时间字符串中获取时间戳?似乎唯一的方法是使用 strtotime,同时暂时覆盖整个 php 应用程序的默认时区,然后立即将其设置回去。这看起来像是一个 hacky 解决方案,如果有更简洁的方法就好了。

编辑:似乎对我正在尝试做的事情存在一些误解。这是一个更具体的例子:

“我想在 America/Los_Angeles 时区内找到与字符串‘next tuesday at 3:00pm’对应的时间戳,并为 $now 指定一个任意值,例如 2014 年 3 月 14 日上午 8:05。”

最佳答案

我准备了一个例子。这可能是你想要的:

<?php

// Including the timezone int the time strings (thanks @Mike B!!!) 
// will make it very easy.  just strtotime() is required

// create a timestamp for March 14th PDT
$now = strtotime('14 March 2014 8:05am America/Los_Angeles');

// get the requested timestamp.
$nexTuesday = strtotime('next tuesday 3:00 pm America/Los_Angeles', $now);

关于php - 从具有特定时区和特定 $now 值的相对日期/时间字符串获取时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14509790/

相关文章:

php - 如何选择过去六个月的销售值?

javascript - 在ajax请求中发送POST变量

mysql - 将 MySQL BigInt(20) unix 纪元时间转换为 datetime(3)

python - 如何在python中转换和减去日期,时间

swift - 如何在考虑夏令时的情况下转换时区之间的时间?

php - 无法将希伯来语发布到 mysql

php - 如何从 url 中删除 %0A

java - Mysql:使用java和hibernate的java.sql.Time数据类型返回java.sql.Date

linux - 如何用日期解析时区

python - python/pytz 从本地时区转换为 UTC 然后返回的问题