开发者

Specify hours in mysql date selection

$datef = (date("Y-m-d",mktime(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year'])));

Out of interest, i'm using this code above to select records from mysql, which b开发者_运维百科its would i have to change to specify hours aswell not just month, day and year. The column type is TIMESTAMP if that helps,

Thanks


You would need to change:

"Y-m-d" to "Y-m-d h-i-s"

If looks like your date manipulation is within the PHP, see here:

http://php.net/manual/en/function.date.php.

However- the question is a little ambiguous. If you are trying to take the value of $datef and use in your SQL, you may want to do this:

$day=$_POST['day'];
$month=$_POST['month'];
$year=$_POST['year'];
$hour=12;
$minute=00;
$second=00;

$datef=strtotime($year.":".$month.":".$day." ".$hour.":".$minute.":".$second);


If you want this to work, you will have to modify your mysql query a little, otherwise, you will only fetch the records with the exact time you specified (e.g.: 12:00:00, but not 12:00:01):

SELECT * FROM records WHERE datefield BETWEEN '2010-11-01 07:00:00' AND '2010-11-01 07:59:59'

or

SELECT * FROM records WHERE datefield LIKE '2010-11-01 07%';
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜