mysql pregmatch,preg_match用于mysql日期格式

im trying to validate a date to see if it matchs the mysql format

this is the code

$match = "/^\d{4}-\d{2}-\d{2} [0-2][0-3]:[0-5][0-9]:[0-5][0-9]$/";

$s = $this->input->post("report_start"). " " . $this->input->post("report_start_time").":00";

$e = $this->input->post("report_end"). " " . $this->input->post("report_end_time").":59";

if($this->input->post("action") != "")

{

echo trim($s). " => " . preg_match($match, trim($s));

echo "
";

echo trim($e). " => " . preg_match($match, trim($e));

}

the date format goes into $s and $e are

$s = 2011-03-01 00:00:00

$e = 2011-03-01 23:59:59

and they both return false (0).

however if i manual inter the date strings into preg_match like

preg_match($match, "2011-03-01 00:00:00")

it works.

i have no idea what im doing wrong

======================

now that i think about it, i only need to validate the houre:min part of the datetime string.

im manually adding the seconds and the date is forced by a datepicker and users cant edit it

解决方案

ok heres wat i did.

since im forcing the date format and the ending seconds of the time part

i just validated the hour:mini part using "/^2[0-3]|[01][0-9]:[0-5][0-9]$";

and if that returns true i put everything together end reconstructed the final datetime string

$match = "/^2[0-3]|[01][0-9]:[0-5][0-9]$/";

$s_d = $this->input->post("report_start");

$s_t = $this->input->post("report_start_time");

$e_d = $this->input->post("report_end");

$e_t = $this->input->post("report_end_time");

if($this->input->post("action") != "")

{

if(

( preg_match($match , trim($s_d." ".$s_t.":00")) )

&& ( preg_match($match , trim($e_d." ".$e_t.":59")) )

)

{

$r = $this->model_report->client_hours_logged(array($s,$e));

$data['report'] = $r;

var_dump($r);

//$this->load->view("report/client_hours_per_client",$data);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值