获取基于字符串的最近(过去)时间


Get the closest (in past) time based on a string

我想创建一个函数,可以找到最近的时间,基于秒的字符串。

系统将收到一个int数,相当于该时间的秒数。PHP必须找到最近的(过去的)日期。

的例子:

//supose that an anterior script created it at "14-08-25 10:32:30"
//and now it's "14-08-25 10:33:12"
$seconds = 30; // the variable passed from an anterior script
$time_received= date('Y-m-d H:i:s'); // this is the time that I'll receive this 
//so, from these 2 variables above, i must find "14-08-25 10:32:30"

有人知道怎么做吗?

我只有这些变量:现在的时间是"14-08-25 10:33:12"$seconds变量

对于这两个,我想得到"14-08-25 10:32:30"

我不完全清楚你想要完成什么,但我通过使用strtotime()来猜测:

<?php
$seconds = 30;
$time = strtotime("-" . $seconds . " seconds");
echo date( "Y-m-d H:i:s", $time );
?>

已找到。我正在使用这个脚本:

$seconds = 30; 
$new_date = new DateTime(date()); //the only two variables that i have
if($new_date->format('s')<$seconds){
    $new_date->setTime($new_date->format('H'),$new_date->format('i')-1,$seconds);
    $old_date = $new_date->format('Y/m/d H:i:s');
}else{
    $new_date->setTime($new_date->format('H'),$new_date->format('i'),$seconds);
    $old_date = $new_date->format('Y/m/d H:i:s');
}