我有两个变量,如:
$date //2011-01-01
$full_old_date //1999-01-01 12:00:00
现在我想将$ full_old_date的新日期和时间合并为一个新变量,如:
$full_new_date //2011-01-01 12:00:00
如何从旧版本中提取时间并将它们组合在一起作为新的日期变量?
答案 0 :(得分:2)
希望这会有所帮助(假设您将这些值设为'字符串'而不是日期/时间戳):
$date = "2011-01-01";
$full_old_date = "1999-01-01 12:00:00"
$full_new_date = $date . ' ' . trim(substr($full_old_date,-8));
//substr() - This gets the last 8 characters of the $full_old_date variables
//trim() - This function removes any whitespace at the edges of the substring
有很多方法可以做到这一点,但这只是一种方法。
答案 1 :(得分:1)
此解决方案可以帮助您
$date ="2011-01-01";
$full_old_date ="1999-01-01 12:00:00";
function newDate($date,$full)
{
return $date . " " . explode(" ",$full)[1];
}
echo newDate($date,$full_old_date);
答案 2 :(得分:1)
您可以像这样使用preg_replace
$date = "2011-01-01";
$full_old_date = "1999-01-01 12:00:00";
echo $new_date = preg_replace('/\d{4}-\d{1,2}-\d{1,2}/', $date, $full_old_date);