如何从数据库中获取日期并将其存储到php中的数组中?

时间:2016-06-08 04:46:03

标签: php mysql arrays date

我一直在研究项目的Leave模块。我想计算一个员工的休假日,不包括国定假日和周末。 因此,我有一个名为出勤的表,其中包含字段AttDate,其中存在员工在场时的日期。 我只想知道如何将数据库日期值导入到数组形式的变量datesOfWorker中作为函数中的预定义。

here is the function

1 个答案:

答案 0 :(得分:0)

以下是我建议您尝试的内容:

    <?php
        // DEFINE SOME DATABASE CONNECTION DATA... COULD BE DONE IN A SEPARATE FILE
        defined("HOST")         or define("HOST",           "localhost");
        defined("DBASE")        or define("DBASE",          "mydb");
        defined("USER")         or define("USER",           "root");        //<== YOUR USERNAME
        defined("PASS")         or define("PASS",           "root");        //<== YOUR PASSWORD

        function getDBH(){
            try {
                $dbHandle = new \PDO('mysql:host='.HOST.';dbname='. DBASE,USER,PASS);
                $dbHandle->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
                return $dbHandle;
            }catch(\PDOException $e){
                throw new \Exception($e->getMessage());
            }
        }

        $dbh        = getDBH();
        $output     = "";
        $sql        = "SELECT ATT.AttDate FROM `attendance` ATT";
        $query      = $dbh->prepare($sql);
        $query->execute();
        $results    = $query->fetchAll(\PDO::FETCH_OBJ);
        $arrDates   = array();

        foreach($results as $intKey=>$objAttDate){
            $arrDates[] = $objAttDate->AttDate;
        }

        // THE ARRAY YOU ARE LOOKING FOR IS $arrDates...

        var_dump($arrDates);