php - 如何比较php中的日期和mysql数据库中的日期?

标签 php html mysql

我有这样的 mysql 表

enter image description here

我的awal_pendaftaran列和akhir_pendaftaran_column有日期类型。

我想将 HTML 选择字段设置为默认选择 nama_periode其中current date (ex:2015-08-2)位于 awal_pendaftaran 之间和 akhir_pendaftaran

我尝试过这样的代码

$date_now = date("Y-m-d");
$perQ="SELECT * FROM periode  where awal_pendaftaran >= CURRENT_DATE ORDER BY awal_pendaftaran";
$perQ2 = mysql_query($perQ) or die(mysql_error());;
$selected="";
while($perF=mysql_fetch_array($perQ2))
{
        $awal_mendaftar=$perF['awal_pendaftaran'];
        $akhir_mendaftar=   $perF['akhir_pendaftaran'];
    if ((strtotime($awal_mendaftar)<= strtotime($date_now)) && (strtotime($akhir_mendaftar)>= strtotime($date_now)) ){
        $selected="selected";
    } else { $selected="";}
    echo "<option value=\"$perF[id_periode]\" selected=\"$selected\" >$perF[nama_periode]</option>\n";

}

但我的 HTML 选择字段始终将“PERIODE 4”显示为所选数据(我想要 PERIODE 2),有什么帮助吗?

最佳答案

在 html 中,所选属性的值是可选的。只要选项具有选定的属性,html 解释器就会将此选项指定为选定的。因此你必须稍微改变你的代码:

if(
    (strtotime($awal_mendaftar)<= strtotime($date_now)) 
    && (strtotime($akhir_mendaftar)>= strtotime($date_now)) 
){
    $selected = '';
} 
else { 
    $selected = 'selected="selected"';
}

echo '<option value="'.$perF['id_periode'].'" '.$selected.'>'.$perF['nama_periode']."</option>\n";

关于php - 如何比较php中的日期和mysql数据库中的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31769768/

相关文章:

html - 使用 css 网格创建马赛克样式网格

javascript - 我该如何解决这个不断扩大的问题?

javascript - 为什么透明对象在动画 Canvas 元素时变得不透明?

python - 将字符串的十六进制字符转换为python中的普通字符

java - 使用现有表的 Hibernate 继承

php - Laravel 异步上传文件

php - 重新安装MAMP PRO后,应用程序找不到数据库

php - 不使用php将西里尔字母发送到mysql数据库

php - Wordpress Docker容器无法上传主题

mysql - (以今天计算)减去(以前一天计算)持续 1 个月