假设我们有开始日期和结束日期
if (isset($_POST['start_date']))
$_POST['start_date'] = gmdate('Y-m-d H:i:s', strtotime($_POST['start_date']));
if (isset($_POST['end_date']))
$_POST['end_date'] = gmdate('Y-m-d H:i:s', strtotime($_POST['end_date']));
$_POST['end_date'] - $_POST['start_date']
会以秒为单位给你过期时间吗?
答案 0 :(得分:3)
不,要以秒为单位获取过期时间,请使用strtotime():
$expired = strtotime($_POST['end_date'])-strtotime($_POST['start_date']);
if (isset($_POST['start_date']))
$_POST['start_date'] = gmdate('Y-m-d H:i:s', strtotime($_POST['start_date']));
if (isset($_POST['end_date']))
$_POST['end_date'] = gmdate('Y-m-d H:i:s', strtotime($_POST['end_date']));
if (isset($_POST['start_date']) && isset($_POST['end_date'])) echo 'Expired time in seconds: ' . $expired;
答案 1 :(得分:-1)
我建议你使用mktime函数从日期文字中获取unix时间戳,以毫秒为单位。
$startDate = mktime($hour, $minute, $second, $month, $day, $year);
$endDate = mktime($hour, $minute, $second, $month, $day, $year);
$diffInSeconds = ($endDate - $startDate) / 1000; // 1 second = 1000 miliseconds