English 中文(简体)
如何将日期字段从理论2 与日期比较?
原标题:How to compare datetime field from Doctrine2 with a date?

我要获得今天由理论2 的 QueryBuilder 创建的项目。 我要比较创建的 At( 日期) 字段和今天的参数( 日期) 。 能否在一个查询中做到这一点?

$qb = $this->createQueryBuilder( i );
$qb->innerJoin( i.type ,  it );
$qb->andWhere( it.name = :type );
$qb->andWhere( i.createdAt < :today );
// i.createdAt == datetime and :today parameter is a date
最佳回答

一个想法是从日期中提取: 年、月、日。然后

$qb->select( p )
   ->where( YEAR(p.postDate) = :year )
   ->andWhere( MONTH(p.postDate) = :month )
   ->andWhere( DAY(p.postDate) = :day );

$qb->setParameter( year , $year)
   ->setParameter( month , $month)
   ->setParameter( day , $day);

周一和一年 你取出理论的扩展

例如:

< a href=>"https://github.com/simukti/DoctrineExtensions" rel=“noreferrer>>DoctrineExtensions

这对我有用,你只需要文件: 日, 日, 月, 月, 月, 年, 年...

你得到的月份,例如:

    $datetime = new DateTime("now");
    $month = $datetime->format( m );
    echo $month;

Copy day.php, month.php and year.php to your bundle XyTestBundleDql Register the new functions in appconfig.yml with

doctrine:


orm:
    auto_generate_proxy_classes: %kernel.debug%
    entity_managers:
        default:
            auto_mapping: true
            dql:
                datetime_functions:
                    month: XyTestBundleDqlMonth
                    year: XyTestBundleDqlYear
                    day: XyTestBundleDqlDay
问题回答

与增加日期的理论延伸相比,有更好的选择。

首先,您需要获得起始日期时间和结束日期时间 :

$today_startdatetime = DateTime::createFromFormat( "Y-m-d H:i:s", date("Y-m-d 00:00:00") );
$today_enddatetime = DateTime::createFromFormat( "Y-m-d H:i:s", date("Y-m-d 23:59:59") );

现在在查询中使用它们 :

$em = end_Registry::get( em );
$qb_1 = $em->createQueryBuilder();
$q_1 = $qb_1->select( wsh )
    ->from( Entitieswishes ,  wsh )
    ->where( wsh.created_at >= :today_startdatetime )
    ->andWhere( wsh.created_at <= :today_enddatetime );


$q_1->setParameter( today_startdatetime , $today_startdatetime);
$q_1->setParameter( today_enddatetime , $today_enddatetime);
$result= $q_1->getQuery ()->getResult ();

也可以使用内置函数 DATE_DIFF( date1, date2) 返回天差数。 check docs

$result = $this->createQueryBuilder( l )
    ->where( DATE_DIFF(l.startDate, CURRENT_DATE()) = 0 )

如此成熟的 ORM 没有提供 < code> DATE 函数, 这是罕见的。 但是, 要从日期字段获取日期, 您可以应用这样的 < code> SUBSTRAING 函数 :

SELECT SUBSTRING(i.dateTimeField,1,10) FROM tableName i

希望它能帮上忙!

您必须在查询中添加 < code> today 参数 。

$today = new DateTime();
$qb->setParameter( today , $today->format( Y-m-d ));

使用 Query 构建器, 您可以用 < code> Y- m- d < /code> 的格式比较日期到 DateTime 的日期

一个容易的解决办法是相应格式化日期时间

public function getWithStartingPremium(DateTime $datetime)
{
    $qb = $this->createQueryBuilder( c );

    return $qb
        ->innerJoin( c.collegePremium ,  cp )
        ->where($qb->expr()->eq( cp.start ,  :date ))
        ->setParameters(array(
             date  => $datetime->format( Y-m-d ),
        ))
        ->getQuery()
        ->getResult();
}

如果您不想为此添加外部扩展名, 您可以将日期时间作为字符串搜索 。

$today = new DateTime();

$qb = $this->createQueryBuilder( i );
$qb->innerJoin( i.type ,  it );
$qb->andWhere( it.name = :type );
$qb->andWhere( i.createdAt LIKE :today );
$qb->setParameter( today , $today->format( Y-m-d ). % );

PS:我知道这是一个老问题, 但我有这个问题,并使用这个解决办法。





相关问题
Weird Time Issue in Python

Problem with using times in Python. Terminal > Python >>> calendar.timegm(datetime.datetime.now().utctimetuple()) 1258449380 This time indicates GMT: Tue, 17 Nov 2009 09:16:20 GMT Eclipse ...

How does a .NET process get the culture information?

I have a Windows service running (C#, .NET 2.0) on Windows Server 2003 R2. In one server the System.Threading.Thread.CurrentThread.CurrentCulture is {en-AU} and in the other {en-US}. This has caused a ...

Best way to get maximum Date value in java?

I m writing a bit of logic that requires treating null dates as meaning forever in the future (the date in question is an expiration date, which may or may not exist). Instead of putting in special ...

Date format in SQL Server

Short Question: What s the best date format to use in SQL Server? Long Explanation: We re converting our database from mysql to SQL Server. In mysql we always used int(11) to avoid the daylight ...

Converting asp.net/sql web app to be ime zone savy

Our current app stores all dates via the server s datetime. I m thinking we need to update all datetime values in the database over to UTC time. Now for displaying these dates back to the user, I ...

Check if string is of SortableDateTimePattern format

Is there any way I can easily check if a string conforms to the SortableDateTimePattern ("s"), or do I need to write a regular expression? I ve got a form where users can input a copyright date (as a ...

DateTime in PropertyGrid in .Net

I m currently working on some DateTime properties in a PropertyGrid in c#.Net. Im using the default drop-down datetime picker. My question: Is there any way to show the full date with the time? By ...

热门标签