php日期转换为秒


php date convert to seconds

我将帖子的日期以y/m/d的形式存储在mysql数据库中,我想检查帖子是否超过3天。有什么办法可以把它转换成秒吗?然后这样检查:

if($mysql_date > $three_days_old){
echo "old post";
}else {
echo "new post";
}

使用date()strtotime():的混合

$myDate = date('Y-m-d', strtotime($mysql_date));
$oldDate = date('Y-m-d', strtotime('3 days ago'));
if ($myDate > $oldDate) {
     echo "old post";
} else {
     echo "new post";
}

Try,strtotime,可以解析各种时间字符串格式。

您可以执行以下操作:

$d1 = strtotime(date('Y-m-d', strtotime($mysql_date)));
$d3 = mktime(0, 0, 0, date('m', $d1), date('d', $d1)-3,  date('Y', $d1));
$d2 = strtotime(date('Y-m-d', strtotime('3 days ago')));
if ($d3 > $d2) {
  echo 'old post';
}else {
  echo 'new post';
}