Php 比较日期(较旧、较新或相同)

Php 比较日期(较旧、较新或相同),php,Php,日期1:年月日 日期2:年月日 日期3:年月日 假设我有: $date1 = 04/07/2014; $date2 = 04/06/2014; $date3 = 04/07/2014; 使用PHP确定日期2是否介于日期1和日期3之间的最有效方法是什么?换句话说,检查日期是否与另一个日期相同、较新或较旧的最佳方法是什么?最好的方法是将日期转换为时间戳,这样比较起来就很容易了。 尝试使用 $time1=标准时间(“日/月/年”); $time2=标准时间(“日/月/年”); 好吧,这真的取决于你

日期1:年月日

日期2:年月日

日期3:年月日

假设我有:

$date1 = 04/07/2014;
$date2 = 04/06/2014;
$date3 = 04/07/2014;

使用PHP确定日期2是否介于日期1和日期3之间的最有效方法是什么?换句话说,检查日期是否与另一个日期相同、较新或较旧的最佳方法是什么?最好的方法是将日期转换为时间戳,这样比较起来就很容易了。 尝试使用

$time1=标准时间(“日/月/年”);
$time2=标准时间(“日/月/年”);

好吧,这真的取决于你使用它的目的,我个人做了什么(在MySQL数据库中检查我制作的日程安排网站的日期时),我使用了以下方法:

    $publishDate = $row['year'] . $row['month'] . $row['day'];
    $now = date("Ynj");//four number year, two number month, two number day (YYYY/MM/DD)
您也可以这样做,将日期输入为Ynj(请参阅日期上的PHP文档) 然后比较它们。这可能有助于将整个想法置于上下文中;这是我为我的网站编写的代码:

  $publishDate = $row['year'] . $row['month'] . $row['day'];
  $now = date("Ynj");//four number year, two number month, two number day (YYYY/MM/DD)

  if($now > $publishDate)
  {
    //This is saying that right now, is greater than when this was added
    $msg = "This date is smaller than the time now, which means it is in the past";
  }

  if ($now < $publishDate)
  {
    echo "This date is greater than the time now, which means it is in the future.";
  }
  if ($now == $publishDate)
  {
    echo "Date is the same";
  }
$publishDate=$row['year']$行['month']$行['day'];
$now=日期(“Ynj”)//四个数字年、两个数字月、两个数字日(YYYY/MM/DD)
如果($now>$publishDate)
{
//这就是说,现在,比添加时更大
$msg=“此日期比现在小,这意味着它是在过去”;
}
如果($now<$publishDate)
{
echo“这个日期比现在大,这意味着它是在未来。”;
}
如果($now==$publishDate)
{
回显“日期相同”;
}

如果你给你的问题增加更多的内容,我相信我可以帮助你分配更多!祝你好运

有几种可能的方法(创建日期对象,重新格式化为可排序的字符串格式,…)——性能方面的“效率(?)很难作为这样一个最小操作的标准,因此在可读性方面使用最适合你的方法。你也可以将它们作为浮动,并检查哪个更大,哪个更小,或者使用基本的if/while语句/循环,这实际上取决于他使用它的目的。(他必须更改为YYYY/MM/DD,还可以使用int)