所以我有一些事件(聚会),每个事件都有一个日期,聚会的时间,我如何将其与当前日期进行比较?我需要找到所有已经过去的事件?有什么想法吗?我甚至不知道从哪里开始,我把日期存储在mysql中,如下所示:dd/mm/yy
,它是纯文本,因为我使用WP metabox。
任何帮助都将不胜感激。
您应该使用DATETIME
格式存储日期并使用
SELECT date FROM dates WHERE DATEDIFF(date,NOW()) <= 0;
如果你坚持保留你非常糟糕的数据库方案,你可以在飞行中进行转换
SELECT STR_TO_DATE(date,'%d/%m/%Y') AS adate WHERE DATEDIFF(adate,NOW()) <= 0
可能是strtotime的一个很好的候选者,它可以将您的日期转换为时间戳,该时间戳可以很容易地与time()函数生成的当前时间戳进行比较。
http://php.net/manual/en/function.strtotime.php
// your db date format is dd/mm/yy (European). the American is mm/dd/yy
$db_date_event = str_replace('/', '-', $db['date_event']);
// when - or . is used in strtotime European d-m-y format is assumed
$db_date_event = strtotime($db_date_event);
if (time() > $db_date_event)
{
echo 'this event is in the past';
}
// To compare at day level: (each day is represented in the 0:00 hrs (day's begin))
if (mktime(0, 0, 0, date('n'), date('j'), date('Y')) > $db_date_event)
{
echo 'this event is in the past';
}
将日期转换为Unix时间戳:
list($d,$m,$y)=explode("/",$datestring);
$date=mktime(0,0,0,$m,$d,$y);
然后与今天相比:
$today=mktime(0,0,0,date("m"),date("d"),date("Y"));
使用<
、==
或>
:
if($date<$today)
{
echo "Date is in the past";
}