在数据库表中只插入月、日和时间的最佳方法是什么?


What is the best way to insert only month, day and time in database table?

我在php/mysqli工作。在这里,我需要创建一个存储日期和时间的表字段。不是年份,只是月、日和时间。我创建了什么样的场?此外,我必须在任何给定的时间内使用存储的数据来与用户当前的日期和时间进行比较。最好的方法是什么呢?

您可以使用类似的东西来分隔日期时间:

$today = date("Y-m-d h:i:sa");
$dateTimeParts = explode(" ", $today);
//first separate date and time:
$date = $dateTimeParts[0];
$time = $dateTimeParts[1];
//separate date's year, month and day
$dateParts = explode("-",$date);

echo "Monthe and day: ".$dateParts[1]. "-" . $dateParts[2]."'n";
echo "Time: ".$time;

然后您可以存储整个日期值(供参考)和dateDay、dateMonth和dateTime等部分。或者,您可以将所有部分内拆并仅存储一列:

$newDate = [];
array_push($newDate, $dateParts[1]); //month
array_push($newDate, $dateParts[2]); //year
array_push($newDate, $time);
$relevantDateParts = implode(" ", $newDate);