如何在同一时间获得不同的日期

时间:2013-11-20 07:28:51

标签: php datetime date-arithmetic

这可能是一个非常简单的问题,但我发现有点难,因为我仍然是PHP的新手。

据说我有:

$time1 = strtotime("today 15:00:00-05:00");

这只是示例,$time1在运行时是动态的,可以是任何值,我想创建具有以下值的新$time2

$time2 = strtotime("this thursday 15:00:00-05:00");

请注意,此处$time1$time2具有相同的15:00:00-05:00,且只有一天不同。所以,为了补充,我有两个输入:

  1. $time1在运行时是动态的。
  2. 字符串this thursday
  3. 如何使用上面的值创建$time2

4 个答案:

答案 0 :(得分:1)

找出“今天”和“今天15:00:00-05:00”之间的区别,并将其添加到“今天星期四”

$sometime = "today 15:00:00-05:00";
$time1 = strtotime($sometime);
// now find only the hour part
$time = mktime(0,0,0, date('n', $time1), date('j', $time1), date('Y', $time1));
$time_difference = $time1 - $time; 
$time2 = strtotime("this thursday") + $time_difference;

甚至更简单:

$sometime = "today 15:00:00-05:00";
$time1 = strtotime($sometime);
// now find only the hour part
$hour_string = date('h:i:s', $time1); 
$time2 = strtotime("this thursday $hour_string");

答案 1 :(得分:0)

不知道为什么你从一开始就不使用PHP的DateTime对象,但是好的:

$dateTime = new DateTime($time1);
$dateTime->modify('+1 day');
$time2 = $dateTime->getTimestamp();

您可以将1更改为任意天数。

答案 2 :(得分:0)

据我所知,你想要的是连续两个字符串“this thursday”和“15:00:00-05:00”:

$str1 = "this thursday";
$str2 = "15:00:00-05:00";

$time = strtotime($str1." ".$str2);

答案 3 :(得分:0)

你可以试试这个。如果不满足要求,您需要指定更多。

 $time1 = date("Y-m-d H:i:s");
 $time2 = 'this thrusday ';
 $time2 .= $time1;
 $time2 = strtotime($time2);