Is it possible to format the following date in an active record where clause?
I need to format channel_titles.entry_date to be in the format Y-m-d, like the $yesterday variable.
$yesterday = date('Y-m-d', strtotime('yesterday'));
$this->db->where('channel_titles.entry_date', $yesterday);
You can use DATE_FORMAT function like that:
$this->db->where("DATE_FORMAT(from_unixtime(channel_titles.entry_date), '%Y-%m-%d')", $yesterday, false);
Try this
$this->db->where("DATE_FORMAT(channel_titles.entry_date,'%Y-%m-%d') ", $yesterday);
Related
I have a MySQL date stored in DATETIME format. So I would like to know how to use date() in my Doctrine QueryBuilder's where clause. For example, 2013-02-01 12:51:17 is the date in MySQL. But I need to retrieve only the date. This is what I have tried:
$qb = $this->getEntityManager()->createQueryBuilder()
->select('t.balance','a.id','t.date')
->from('TestMainBundle:Transaction','t')
->groupBy('a.id')
->orderBy('a.id')
->where("t.date in date('t.date') ");
return $qb->getQuery()->getResult();
I received the following error:
QueryException: [Syntax Error]: Error: Expected Doctrine\ORM\Query\Lexer::T_OPEN_PARENTHESIS, got 'date'
Hi You can use SUBSTRING to fix your probleme
->where('SUBSTRING(t.date, 1, 10) IN (:param)')
->setParameter('param', array('2017-04-06'))
As it is pointed out in the comments, you cannot use mysql-specific date() or date_format() functions in Doctrine.
But for the particular case of searching a certain date in the datetime field, you can treat a date like a string, and thus use LIKE operator
->Where('t.date LIKE :date')
->setParameter(':date', "$date%")
As of
But I need to retreive only the date
you just format the returned value using format("Y-m-d") method. i.e.
echo $row->getDate()->format("Y-m-d");
You don't need the "date" in your where clause.
Juste remove it like this :
->where('t.date in (:yourwanteddate)')
->setParameter('yourwanteddate', '2013-02-01 12:51:17');
I have a column with datetime data type and I want to build a SQL query in Zend Framework2 which compare date part with user input date.
Need to build similar part as DATE(datetime column) = '2014-09-16' with;
$select->where();
would be very grateful if someone could help on this.
Use like this:
$date = '2014.05.24';
$select->where('date(expecting_date) = "'.$date.'"');
You should use predicate expression for these kind of conditions, like :
$select = new \Zend\Db\Sql\Select(table name);
$select->where(new \Zend\Db\Sql\Predicate\Expression('DATE(datetime) = ?', '2014-09-16'));
function details_klanten($idKlant,$start,$eind){
$this->db->select(' Project.idProject,
Project.Titel,
Project.idProjecttype,
Project.Begindatum,
Project.Deadline,
Project.idKlant,
Projecttypes.idProjecttypes,
Projecttypes.Type,
Werknemer.idWerknemer,
Werknemer.Voornaam,
Statusproject.idStatusProject,
Statusproject.Soort,
Klant.Naam');
$this->db->order_by('Titel', 'asc');
$this->db->from('Project');
$this->db->join('Klant', 'Klant.idKlant = Project.idKlant');
$this->db->join('Projecttypes', 'Projecttypes.idProjecttypes = Project.idProjecttype');
$this->db->join('Werknemer', 'Werknemer.idWerknemer = Project.idWerknemer');
$this->db->join('Statusproject', 'Statusproject.idStatusProject = Project.idStatusProject');
if ($idKlant > 0){
$this->db->where('Klant.idKlant',$idKlant);
$this->db->where('Project.Begindatum >',$start);
$this->db->where('Project.Deadline <',$eind);
}
$query = $this->db->get();
if($query->num_rows()>0){
return $query->result();
}
else{
return false;
}
}
The project.Begindatum and the Project.Deadline are varchar(10). So it looks at the first two numbers not the full date. For example:
$start = '01-04-2014';
'Project.Begindatum' = (Varchar)'02-03-2014'.
Then it will be shown because
it looks only to the '01'(-04-2014) and the '02'(-03-2014)
Use mysql's STR_TO_DATE() function and also pass third parameter in where() as FALSE
$this->db->where("STR_TO_DATE(Project.Begindatum,'%d-%m-%Y') >",$start,FALSE);
$this->db->where("STR_TO_DATE(Project.Deadline,'%d-%m-%Y') <",$eind,FALSE);
Its better to change the type of your columns to store in standard format use date type to store dates in database ,Using STR_TO_DATE with format %d-%m-%Y then the value stored in table should have %d-%m-%Y format otherwise it won't work ,it should be compared to $start = '2014-04-01';,otherwise you need another function i.e DATE_FORMAT to format it like 01-04-2014
$this->db->where("DATE_FORMAT(STR_TO_DATE(Project.Begindatum,'%d-%m-%Y'),'%d-%m-%Y') >",
$start,FALSE);
$this->db->where("DATE_FORMAT(STR_TO_DATE(Project.Deadline,'%d-%m-%Y'),'%d-%m-%Y') <"
,$eind,FALSE);
Reminder: STR_TO_DATE(...) output format: Y-m-d
Example: 2020-12-24
Therefore: $start and $eind format: Y-m-d
Example: 2020-12-31
$this->db->where("STR_TO_DATE(Project.Begindatum, '%m-%d-%Y') >",$start);
$this->db->where("STR_TO_DATE(Project.Deadline, '%m-%d-%Y') <",$eind);
I hope this helps you too. Peace.
I am using a DATE field in my MYSQL table, and pulling it through on a php page. The problem is it comes out as "2011-04-23"
Is there a way I can reformat this as 23/04/2011?
Thanks :)
date("d/m/Y", strtotime("2011-04-23"));
that should do it
date()
strtotime()
DATE_FORMAT(date,format)
Look here: http://dev.mysql.com/doc/refman/5.0/es/date-and-time-functions.html
Assuming variable $date contains your MySQL data:
$date = '2011-04-23';
$timezone = 'Europe/London'; // this is optional argument
$formatted = DateTime::createFromFormat('Y-m-d', $date, new DateTimeZone($timezone));
// or without the optional timezone - where php will assume the default timezone from your OS
$formatted = DateTime::createFromFormat('Y-m-d', $date);
echo $formatted->('d/m/Y');
All,
I need to retrieve the date in some specific format in MYSQL.
For eg,
it should return month/year(10/2009).
My MYSQL version is 5.1.
Thanks,
Srinivasan.
DATE_FORMAT function
DATE_FORMAT(col,'%m/%Y')
date_format(yourdatefield, '%c/%Y') as formatted_date
will do that- http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
use DATE_FORMAT function
http://www.mysqlformatdate.com will help you use the function properly
Check the documentation for this function in MySQL
DATE_FORMAT(date, format)
DATE_FORMAT(col, '%m/%Y')
For IIS, I just use a function:
FUNCTION mySLQDate(xdate)
IF xdate <> "" THEN
mySLQDate = year(xdate) & "/" & month(xdate) & "/" & day(xdate)
END IF
END FUNCTION