如何到月份日期之后的那一天

时间:2015-04-09 04:58:30

标签: sql postgresql cakephp cakephp-2.0

因此我将根据所选月份创建搜索数据,因此问题是当2月份的日期为29时,我希望在月份日期之后的那一天。我使用cakephp和数据库postgresql

form.ctp

<legend>SUMMARY TRANSAKSI</legend>
<div id="tabs">
                <?php echo $this->Form->create('Transaction',array('action'=>'graph_pln_postpaid_month')) ?>
                <?php
                    $now = new DateTime();
                    $years = $now->format("Y");
                ?>
                <table cellpadding="0px" cellspacing="5px" width="70%">
                    <tr>
                        <td>Parameter</td>
                        <td><select name=data[Transaction][month_start] class=form-control style=width:150px;>
                                <option></option>
                                <option value="01-01-<?php echo $years;?>">Januari</option>
                                <option value="01-02-<?php echo $years;?>">Februari</option>
                                <option value="01-03-<?php echo $years;?>"Maret</option>
                                <option value="01-04-<?php echo $years;?>">April</option>
                                <option value="01-05-<?php echo $years;?>">Mei</option>
                                <option value="01-06-<?php echo $years;?>">Juni</option>
                                <option value="01-07-<?php echo $years;?>">Juli</option>
                                <option value="01-08-<?php echo $years;?>">Agustus</option>
                                <option value="01-09-<?php echo $years;?>">September</option>
                                <option value="01-10-<?php echo $years;?>">Oktober</option>
                                <option value="01-11-<?php echo $years;?>">November</option>
                                <option value="01-12-<?php echo $years;?>">Desember</option>
                            </select>
                        </td>
                        <td>
                            <select name=data[Transaction][month_end] class=form-control>
                                <option></option>
                                <option value="31-01-<?php echo $years;?>">Januari</option>
                                <option value="28-02-<?php echo $years;?>">Februari</option>
                                <option value="30-03-<?php echo $years;?>">Maret</option>
                                <option value="31-04-<?php echo $years;?>">April</option>
                                <option value="30-05-<?php echo $years;?>">Mei</option>
                                <option value="31-06-<?php echo $years;?>">Juni</option>
                                <option value="30-07-<?php echo $years;?>">Juli</option>
                                <option value="31-08-<?php echo $years;?>">Agustus</option>
                                <option value="30-09-<?php echo $years;?>">September</option>
                                <option value="31-10-<?php echo $years;?>">Oktober</option>
                                <option value="30-11-<?php echo $years;?>">November</option>
                                <option value="31-12-<?php echo $years;?>">Desember</option>
                            </select>
                        </td>



                    </tr>
                    <tr>
                        <table>
                            <tr>
                                <td><?php echo $this->Form->submit('Lihat Grafik', array('class' => 'btn btn-large btn-primary')); ?>
                                     <?php echo $this->Form->end(); ?>
                                </td>
                                <?php echo $this->Form->create('Transaction',array('action'=>'graph_pln_postpaid_month/flush','id'=>'flush')) ?>
                                <td>
                                    <?php echo $this->Form->submit('Bersihkan Filter', array('class' => 'btn btn-large btn-primary')) ?>
                                    <?php echo $this->Form->end() ?>
                                </td>
                            </tr>
                        </table>
                    </tr>
                </table>

控制器

if ($id=='flush') {
        $this->Session->delete('transactions_pln_postpaid_month.month_start');
        $this->Session->delete('transactions_pln_postpaid_month.month_end');
    }

    //for write start date
    if($this->request->data['Transaction']['month_start']){
        $conditions["Transaction.create_date >="] = date("d-M-Y",strtotime($this->request->data['Transaction']['month_start']))." 00:00:00";
        $this->Session->write('transactions_pln_postpaid_month.month_start', $conditions["Transaction.create_date >="]);
    }else{
        $conditions["Transaction.create_date >="] = date("d-M-Y")." 00:00:00";
        $this->request->data['Transaction']['month_start'] = date("d-M-Y", strtotime($this->request->data['Transaction']['month_start']." 00:00:00"));
    }

    //for write end date
    if($this->request->data['Transaction']['month_end']){
        $conditions["Transaction.create_date <="] = date("d-M-Y",strtotime($this->request->data['Transaction']['month_end']))." 23:59:59";
        $this->Session->write('transactions_pln_postpaid_month.month_end', $conditions["Transaction.create_date <="]);
    }else{
        $conditions["Transaction.create_date <="] = date("d-M-Y")." 23:59:59";
        $this->request->data['Transaction']['month_end'] = date("d-M-Y", strtotime($this->request->data['Transaction']['month_end']." 00:00:00"));
    }


    //read session start date
    if($this->Session->read('transactions_pln_postpaid_month.month_start')!=NULL) {
        $conditions["Transaction.create_date >="] = $this->Session->read('transactions_pln_postpaid_month.month_start');
        $this->request->data['Transaction']['month_start'] = date("d-M-Y", strtotime($this->Session->read('transactions_pln_postpaid_month.month_start')));
    }

    //read session end date
    if($this->Session->read('transactions_pln_postpaid_month.month_end')!=NULL) {
        $conditions["Transaction.create_date <="] = $this->Session->read('transactions_pln_postpaid_month.month_end');
        $this->request->data['Transaction']['month_end'] = date("d-M-Y", strtotime($this->Session->read('transactions_pln_postpaid_month.month_end')));
    }

    $this->Transaction->recursive = -1;
$report_posts = $this->Transaction->find('all', array(
        'conditions' => array_merge($conditions,array('Transaction.product_id=100')),
        'joins' => array(
            array(
                'table' => 'users',
                'alias' => 'User',
                'type' => 'LEFT OUTER',
                'conditions' => array('User.id = Transaction.user_id')
            ),
            array(
                'table' => 'users',
                'alias' => 'B',
                'type' => 'FULL OUTER',
                'conditions' => array('B.id = User.bank')
            ),
            array(
                'table' => 'inboxes',
                'alias' => 'Inbox',
                'type' => 'LEFT',
                'conditions' => array('Inbox.id = Transaction.inbox_id')
            ),
            array(
                'table' => 'pln_postpaids',
                'alias' => 'PLNPostpaid',
                'type' => 'LEFT',
                'conditions' => array('Inbox.id = PLNPostpaid.inbox_id')
            )
        ),
        'fields' => array(
            'B.company',
            'User.company',
            'Transaction.create_date',
            'sum(cast("PLNPostpaid"."jumlahrek" as integer)) as jumlah',
            'sum(Transaction.price_buy) as jumbuy',
            'sum(("Transaction"."price_sell")-("Transaction"."price_buy")) as admin'
        ),
        'group' => array('B.company','Transaction.id','User.company')
    ));
    $this->set('report_posts',$report_posts);

1 个答案:

答案 0 :(得分:1)

(猜测,因为我无法理解你的解释。你应该真的展示一些样本数据预期结果。)

我认为你正在寻找本月的最后一天。

如果是这样,对于给定日期,请获取其所在月份的最后一天:

SELECT (date_trunc('month', DATE '2014-05-02') + INTERVAL '1' MONTH) - INTERVAL '1' DAY;