开发者

comparison between two times

I am developing a website on php. I am submitting a time field as M:SAM or PM (EX: 10:55AM) something like this. I want to compare this field with current time. I am getting the current time by using this code.

$time = date("h:i A");
$time = str_replace(' ','',$time); 
开发者_运维百科

But here I want to compare this current time with database result.

Thanks Sateesh.


If you have 10:55AM you can convert it to timestamp:

$input     = '10:55PM';
$timestamp = strtotime($input);
$output    = date('H:i', $timestamp);

// output is 22:55 (because of PM in my example so it's correct)


$a = DateTime::createFromFormat('h:iA', $time1);
$b = DateTime::createFromFormat('h:iA', $time2);
if ($a < $b) {
  // $a before $b
}

This one expects both dates as 12:34AM. You can change the format (separately) like you want. Have a look at DateTime::createFromFormat()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜