php - MySQL Where Date (not datetime) as Week of year

标签 php mysql date symfony doctrine

我有以下使用 Symfony2 的查询,我想转换 i.fechaIniciodate 字段 Y-m-d (2013-08-31 ) 到当前周数,以便我可以从当前周获取结果...

$now = new \DateTime();
$semana = $now->format('W'); // Week Number
$query = $this->getEntityManager()
->createQuery("
        SELECT i
        FROM PgeIncidenciasBundle:Incidencia i
        WHERE i.fechaInicio = :semana // This is wrong because compares
                                      // Y-m-d with W so no results found...
        ")
->setParameters(array(
    'semana' => $semana
))
;
try {
    return $query->getResult();
}
catch (\Doctrine\ORM\NoResultException $e)
{
    return null;
}

最佳答案

使用BETWEEN运算符(operator):

$from = new DateTime;
$from->setIsoDate($from->format('o'), $from->format('W'));
$to = clone $from;
$to->modify('+6 day');

$query = $this->getEntityManager()
    ->createQuery("
        SELECT i
        FROM PgeIncidenciasBundle:Incidencia i
        WHERE i.fechaInicio BETWEEN :from AND :to
    ")
    ->setParameters(array(
        'from' => $from->format('Y-m-d'),
        'to' => $to->format('Y-m-d'),
    ));

关于php - MySQL Where Date (not datetime) as Week of year,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18549182/

相关文章:

php - 云驱动器与云文件(或者我们不应该打扰吗?)

php - 从单个列存储和调用时间戳(或一般数据)

php - 如何在strtotime中写一个full time?

mysql - 为电视节目创建数据库的方法

带有 mysql 数据库的 C# 应用程序,日期保存为字符串

php - 检索和保存数据

javascript - Laravel - 使用 AJAX 过滤数据库的能力

mysql - AWS Data Pipeline MySQL Nulls sed shell 命令事件 MIGRAINE

java - SimpleDateFormat 解析和不明确的日期格式

c# - 无法将 MySQL 日期/时间值转换为 System.DateTime