在PHP中执行与datetime相关的操作

时间:2008-09-11 13:48:28

标签: php sql postgresql datetime

您如何实际执行日期时间操作,例如添加日期,查找差异,找出在某个时间间隔内排除周末的天数?我个人开始将这些操作中的一些传递给我的postgresql dbms,因为通常我只需要发出一个sql语句来获得答案,但是,用PHP方式来做这件事我将不得不写更多的代码意味着更多的机会发生错误......

PHP中是否有任何库以不需要大量代码的方式执行日期时间操作?在'给定两个日期,两个日期之间有多少个工作日?在SQL或$ pet_lang'中实现,通过进行此查询来解决?

SELECT  COUNT(*) AS total_days
FROM    (SELECT date '2008-8-26' + generate_series(0,
          (date '2008-9-1' - date '2008-8-26')) AS all_days) AS calendar
WHERE   EXTRACT(isodow FROM all_days) < 6;

12 个答案:

答案 0 :(得分:5)

对于大多数日期时间操作,我通常会转换为Unixtime并对Unixtime整数执行加法减法等,您可能需要查看Zend框架Zend_Date类。

这有很多你描述的功能。虽然Zend被称为“框架”,但它作为一个类库来挑选和选择元素非常好。我们经常将它包含在项目中,然后在我们需要它们时立即引入它们。

答案 1 :(得分:2)

PEAR::Date看起来可能有一些有用的功能。

PEAR::Calendar也可能有用。

答案 2 :(得分:2)

strtotime()很有用,但它确实有一些奇怪的行为,如果你不只是用它来转换格式化的日期/时间字符串,它会不时弹出。

“+1月”或“-3天”之类的内容有时无法满足您的预期输出。

答案 3 :(得分:2)

PHP5 +的DateTime对象很有用,因为它是跳跃时间和 夏令时意识到,但它真的需要一些扩展 解决这个问题。我写了以下内容来解决类似的问题。 find_WeekdaysFromThisTo()方法非常强大,但如果您的时间跨度不到2年,它的工作速度相当快。

$tryme = new Extended_DateTime('2007-8-26');
$newer = new Extended_DateTime('2008-9-1');

print 'Weekdays From '.$tryme->format('Y-m-d').' To '.$newer->format('Y-m-d').': '.$tryme -> find_WeekdaysFromThisTo($newer) ."\n";
/*  Output:  Weekdays From 2007-08-26 To 2008-09-01: 265  */
print 'All Days From '.$tryme->format('Y-m-d').' To '.$newer->format('Y-m-d').': '.$tryme -> find_AllDaysFromThisTo($newer) ."\n";
/*  Output:  All Days From 2007-08-26 To 2008-09-01: 371   */
$timefrom = $tryme->find_TimeFromThisTo($newer);
print 'Between '.$tryme->format('Y-m-d').' and '.$newer->format('Y-m-d').' there are '.
      $timefrom['years'].' years, '.$timefrom['months'].' months, and '.$timefrom['days'].
      ' days.'."\n";
/*  Output: Between 2007-08-26 and 2008-09-01 there are 1 years, 0 months, and 5 days. */

class Extended_DateTime extends DateTime {

    public function find_TimeFromThisTo($newer) {
        $timefrom = array('years'=>0,'months'=>0,'days'=>0);

        // Clone because we're using modify(), which will destroy the object that was passed in by reference
        $testnewer = clone $newer;

        $timefrom['years'] = $this->find_YearsFromThisTo($testnewer);
        $mod = '-'.$timefrom['years'].' years';
        $testnewer -> modify($mod);

        $timefrom['months'] = $this->find_MonthsFromThisTo($testnewer);
        $mod = '-'.$timefrom['months'].' months';
        $testnewer -> modify($mod);

        $timefrom['days'] = $this->find_AllDaysFromThisTo($testnewer);
        return $timefrom;
    } // end function find_TimeFromThisTo


    public function find_YearsFromThisTo($newer) {
        /*
        If the passed is:
        not an object, not of class DateTime or one of its children,
        or not larger (after) $this
        return false
        */
        if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))
            return FALSE;
        $count = 0;

        // Clone because we're using modify(), which will destroy the object that was passed in by reference
        $testnewer = clone $newer;

        $testnewer -> modify ('-1 year');
        while ( $this->format('U') < $testnewer->format('U')) {
            $count ++;
            $testnewer -> modify ('-1 year');
        }
        return $count;
    } // end function find_YearsFromThisTo


    public function find_MonthsFromThisTo($newer) {
        /*
        If the passed is:
        not an object, not of class DateTime or one of its children,
        or not larger (after) $this
        return false
        */
        if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))
            return FALSE;

        $count = 0;
        // Clone because we're using modify(), which will destroy the object that was passed in by reference
        $testnewer = clone $newer;
        $testnewer -> modify ('-1 month');

        while ( $this->format('U') < $testnewer->format('U')) {
            $count ++;
            $testnewer -> modify ('-1 month');
        }
        return $count;
    } // end function find_MonthsFromThisTo


    public function find_AllDaysFromThisTo($newer) {
        /*
        If the passed is:
        not an object, not of class DateTime or one of its children,
        or not larger (after) $this
        return false
        */
        if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))
            return FALSE;

        $count = 0;
        // Clone because we're using modify(), which will destroy the object that was passed in by reference
        $testnewer = clone $newer;
        $testnewer -> modify ('-1 day');

        while ( $this->format('U') < $testnewer->format('U')) {
            $count ++;
            $testnewer -> modify ('-1 day');
        }
        return $count;
    } // end function find_AllDaysFromThisTo


    public function find_WeekdaysFromThisTo($newer) {
        /*
        If the passed is:
        not an object, not of class DateTime or one of its children,
        or not larger (after) $this
        return false
        */
        if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))
            return FALSE;

        $count = 0;

        // Clone because we're using modify(), which will destroy the object that was passed in by reference
        $testnewer = clone $newer;
        $testnewer -> modify ('-1 day');

        while ( $this->format('U') < $testnewer->format('U')) {
            // If the calculated day is not Sunday or Saturday, count this day
            if ($testnewer->format('w') != '0' && $testnewer->format('w') != '6')
                $count ++;
            $testnewer -> modify ('-1 day');
        }
        return $count;
    } // end function find_WeekdaysFromThisTo

    public function set_Day($newday) {
        if (is_int($newday) && $newday > 0 && $newday < 32 && checkdate($this->format('m'),$newday,$this->format('Y')))
            $this->setDate($this->format('Y'),$this->format('m'),$newday);
    } // end function set_Day


    public function set_Month($newmonth) {
        if (is_int($newmonth) && $newmonth > 0 && $newmonth < 13)
            $this->setDate($this->format('Y'),$newmonth,$this->format('d'));
    } // end function set_Month


    public function set_Year($newyear) {
        if (is_int($newyear) && $newyear > 0)
            $this->setDate($newyear,$this->format('m'),$this->format('d'));
    } // end function set_Year
} // end class Extended_DateTime

答案 4 :(得分:2)

要添加日期,您可以使用方法 DateTime :: add 向DateTime对象添加天数,月数,年数,小时数,分钟数和秒数),从php 5.3.0开始提供。

要找到两个日期之间的差异,可以使用 DateTime :: diff 方法;但似乎没有办法计算两个日期之间的工作日。

答案 5 :(得分:1)

最简单的方法是使用时间戳,表示自2008年1月1日以来的秒数。使用时间戳类型,您可以执行以下操作:

now = time();
tomorrow = now + 24 * 60 * 60; // 24 hours * 60 minutes * 60 seconds

在php网页上查看time()date()mktime()的文档。这些是我最常使用的三种方法。

答案 6 :(得分:0)

您可以结合使用strtotimemktimedate算术

这是一个使用组合todo一些算术的例子http://rushi.wordpress.com/2008/04/13/php-print-out-age-of-date-in-words/我将在这里重现代码以简化     

if ($timestamp_diff < (60*60*24*7)) {
   echo floor($timestamp_diff/60/60/24)." Days";
} elseif ($timestamp_diff > (60*60*24*7*4)) {
   echo floor($timestamp_diff/60/60/24/7)." Weeks";
} else {
   $total_months = $months = floor($timestamp_diff/60/60/24/30);
   if($months >= 12) {
      $months = ($total_months % 12);
      $years&nbsp; = ($total_months - $months)/12;
      echo $years . " Years ";
   }
   if($months > 0)
      echo $months . " Months";
}
?>

答案 7 :(得分:0)

@Rushi我不喜欢strtotime()个人..我不知道为什么但是我今天早上发现传递像'2008-09-11 9:5 AM'这样的字符串返回strtotime返回错误。 ..

我不认为您提供的代码解决了示例问题'鉴于两个日期,两个日期之间有多少个工作日?在SQL或$ pet_lang'中实现,我没有考虑是否有公共假期列表...

答案 8 :(得分:0)

如果查看http://php.net/date,您会发现一些使用mktime()执行操作的示例。

一个简单的例子是锻炼明天的日期。您可以通过简单地将{1}添加到mktime()中的日期值来执行此操作,如下所示:

$tomorrow = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") + 1, date("Y")));

所以在这里,您将收到包含明天日期的YYYY-MM-DD形式的日期。您还可以通过简单地将“+”替换为“ - ”来减去天数。 mktime()使生活变得更加轻松,并使您免于必须执行嵌套if语句和其他此类麻烦的编码。

答案 9 :(得分:0)

您可以获得两个日期之间的天数,如下所示:

$days = (strtotime("2008-09-10") - strtotime("2008-09-12")) / (60 * 60 * 24);

你可以制作类似的功能(我的工作电脑没有安装php,所以我不能保证语法100%正确)

function isWorkDay($date)
{
 // check if workday and return true if so
}

function numberOfWorkDays($startdate, $enddate)
{
  $workdays = 0;
  $tmp = strtotime($startdate);
  $end = strtotime($enddate);
  while($tmp <= $end)
  {
    if ( isWorkDay( date("Y-m-d",$tmp) ) ) $workdays++;
    $tmp += 60*60*24;
  }
  return $workdays;
}

如果你不喜欢strtotime并且你总是有相同格式的日期,你可以使用像

这样的爆炸功能
list($year, $month, day) = explode("-", $date);

答案 10 :(得分:0)

我强烈建议在进行日期计算时使用PHP 5.2的DateTime objects,而不是使用UNIX时间戳。当您使用返回UNIX时间戳的PHP日期函数时,您可以使用非常有限的范围(例如1970年之前没有任何内容)。

答案 11 :(得分:-1)

获得工作日/假期,postgresql CTE ftw - 请参阅http://osssmb.wordpress.com/2009/12/02/business-days-working-days-sql-for-postgres-2/

相关问题