在平常写项目的时候,不可避免的会用到时间计算。
如果只是简单的计算的话,只需要将时间字符串转换为时间戳然后对比即可。

// 时间1
$date1 = strtotime('2018-10-01'); // 1538352000
// 时间2
$date2 = strtotime('2018-11-01'); // 1541030400
// 时间2 比 时间1多多少秒
$diff = $date2 - $date1; // 2678400

http://php.net/manual/zh/datetime.diff.php

$birthday = new \DateTime('1996-10-11');
$now = new \DateTime();
$interval = $birthday->diff($now);
object(DateInterval)[18]
  public 'y' => int 21
  public 'm' => int 6
  public 'd' => int 0
  public 'h' => int 17
  public 'i' => int 16
  public 's' => int 2
  public 'weekday' => int 0
  public 'weekday_behavior' => int 0
  public 'first_last_day_of' => int 0
  public 'invert' => int 0
  public 'days' => int 7852
  public 'special_type' => int 0
  public 'special_amount' => int 0
  public 'have_weekday_relative' => int 0
  public 'have_special_relative' => int 0

标签: none

添加新评论