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


How to get different date with the same time

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

大概我有:

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

这只是一个例子,$time1在运行时是动态的,可以是任何值,我想创建一个新的$time2,它的值为:

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

请注意,$time1$time215:00:00-05:00相同,只是日期不同。所以,为了收集,我有两个输入:

  1. CCD_ 6在运行时是动态的
  2. 字符串CCD_ 7

如何创建具有上述值的$time2

找出"today"answers"today 15:00:00-05:00"之间的区别,并将其添加到"this thursday"中

$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");

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

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

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

据我所知,您想要的是连接两个字符串"this thursday"answers"15:00:00-05:00":

$str1 = "this thursday";
$str2 = "15:00:00-05:00";
$time = strtotime($str1." ".$str2);

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

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