我有这种方式在数据库中保存日期:
(下拉)月(下拉)日(下拉)年
我有这样的方式来节省数据库的时间:
(文本框)时间(下拉)AM/PM
则会像这样保存在目录中:
+------------------+-----------+
| date | time |
+------------------+-----------+
| January 1, 2013 | 4:00 PM |
+------------------+-----------+
| January 6, 2013 | 9:00 AM |
+------------------+-----------+
如何用if else语句比较数据库中的日期和计算机时间?
if ("date/time in database" == "date/time in computer")
{
echo "something stuff.";
}
使用strtotime()
if (strtotime("January 1, 2013") == strtotime(date('F j, Y')))
{
echo "something stuff.";
}
文档链接:http://www.php.net/manual/en/function.strtotime.php
祝你好运! !
将日期转换为unix时间戳,然后进行比较:
if (strtotime($your-db-stored-date) == strtotime($now)) {
//doSomething;
}
无论如何,一定要在数据库中使用适当的类型保存日期…
不是确切的答案,但它可以帮助你…我使用这些函数将datetime作为UTC存储在数据库中,并在显示时转换为服务器时间。
function convert_server_datetime_to_utc() {
return $gmdate = gmdate("Y-m-d H:i:s");
}
function convert_utc_to_server_datetime($utc=false) {
if (empty($utc)){return false;}
$date = new DateTime($utc);
$hours_offset = date("Z")/60/60;
if(substr($hours_offset, 0, 1) == "-") {
$hours_offset = substr($hours_offset, 1);
$invert=1;
}
$offset = new DateInterval('PT'.$hours_offset.'H');
if (!empty($invert)) {$offset->invert = 1;}
$date->add($offset);
return $date->format("Y-m-d H:i:s");
}
echo "convert_datetime_to_utc(): " . $d = convert_server_datetime_to_utc();
echo "<hr>convert_utc_to_datetime(): " . convert_utc_to_server_datetime($d);
$date_from_db = "January 1, 2013 4:00 PM"; //just concatenate the date and time from db
if ($date_from_db == date('F j, Y h:i A')){
echo "write your code";
}
OR
if (strtotime($date_from_db) == strtotime(date('F j, Y h:i A'))){
echo "write your code";
}