将日期检查插入函数

时间:2015-07-19 08:15:01

标签: php postgresql function

我有这个函数从名为'customer'的表中加载一个数组,它工作正常,但我也希望它检查客户是否已过期,如果是,则显示错误

所以我在下面有代码来检查id_expiry_date是否已经结束,但是我很难找到将它放在函数中的位置。尝试了一些事情,但似乎只是打破了。请协助。

    function customer_load_by_id($id)
{
$dbh = dbh_get();

$id = array();

$sql = 'select c.* ' .
         'from customer c ' .
        'where c.cust_id = (select max(cust_id) from customer where id = ?)';
$stmt = $dbh->prepare($sql);
$stmt->execute(array($id));
$r = $stmt->fetch();
if (!is_bool($r)) {
    $fields = id_get_fields();
    foreach ($fields as $field) {
        $n = $field['name'];
        $id[$n] = $r[$n];
    }
}
dbh_free($dbh);

$exp_date = "$id_expiry_date";
$todays_date = date("Y-m-d");

$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);

if ($expiration_date > $today) {

//continue happily

} else

 {
//don't continue
}
return $id;
}

// One of the fields in the array above is id_expiry_date

1 个答案:

答案 0 :(得分:0)

我真的不了解你的SQL架构,但是,使用Pomm,我会写下面的代码:

<?php
use PommProject\Foundation\Pomm;
$loader = require __DIR__ . '/vendor/autoload.php'; //<- setup autoloading
$pomm = new Pomm(…); // <- connection params go there
$sql = <<<"SQL"
select 
  c.*,
  c.expiry_date < now() as is_expired 
from customer c 
where c.cust_id = (select max(cust_id) from customer where id = $*)
SQL;
$result = $pomm
    ->getDefaultSession()
    ->getQueryManager()
    ->query($sql, [$id])
    ->current() // <- Take the first row if exist
    ;

if ($result) {
    if ($result['is_expired']) { //<- 'is_expired' is converted as boolean
        echo "expired";
    } else {
        echo "not expired";
    }
} else {
   echo "no result";
}