我知道在 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/