"andWhere" clause doesn't work Doctrine2 - php

I want to get some data from database table, where date field is in range between two dates.
Here is my code below:
$date = new \DateTime('today');
$newDate = new \DateTime('today');
$newDate->add(new \DateInterval('P2D'));
$query = $repository->createQueryBuilder('s')
->select('s.day')
->where('s.day > :data')
->andWhere('s.day < :newDate')
->setParameter('data', $date)
->setParameter('newDate', $newDate)
->getQuery();
$dates = $query->getResult();
But unfortunately it doesn't work.
The second method gives empty array, too.
$date = new \DateTime('today');
$newDate = new \DateTime('today');
$newDate->add(new \DateInterval('P2D'));
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT s.day
FROM AppBundle:Seance s
WHERE s.day BETWEEN :date AND :newDate')
->setParameter('date', $date)
->setParameter('newDate', $newDate);
$seances= $query->getResult();
If I remove andWhere clause or between, doctrine returns data correctyly but all recordse
Does anyone have idea why it doesn't work?

In my application I do what you are asking, using the following:
/* #var $date1 \DateTime */
$date1 = \DateTime::createFromFormat('d/m/Y', $this->request()->get("from_date"));
$date1->format('Y-m-d h:i:s');
$date1->setTime(0, 0, 0); // You'll likely not need this bit if times don't matter to you
/* #var $date2 \DateTime */
$date2 = \DateTime::createFromFormat('d/m/Y', $this->request()->get("to_date"));
$date2->format('Y-m-d h:i:s');
$date2->setTime(23, 59, 59); // You'll likely not need this bit if times don't matter to you
Then in it's repository we do this:
->andWhere("s.date >= :date1 AND s.date < :date2)

You simply needed to pass correct criteria.
$dateFrom = (new \DateTime())->setTime(0, 0, 0);
$dateTo = (new \DateTime())->add(new \DateInterval('P2D'))->setTime(23, 59, 59);
$query = $repository->createQueryBuilder('s')
->where('s.day >= :dateFrom', 's.day <= :dateTo')
->setParameter('dateFrom', $dateFrom)
->setParameter('dateTo', $dateTo)
->getQuery();
$dates = $query->getResult();

Related

PHP giving fatal error on comparing dates

I have one date in database, and I want to compare it with the current date. So I write the following function:
$today = new DateTime();
$today_date = $current_date->format('Y-m-d H:i:s');
function do_diifernce($date_1, $date_2) {
$my_date = $date_1;
$createDate = new DateTime($my_date);
$strip = $createDate->format('Y-m-d');
$difference = $date_2->diff($createDate, true);
$difference->total_difference = $difference->y . "." . $difference->m;
return $difference;
}
$comparison = do_diifernce($databse_date, $today_date);
So
$databse_date = 2019-06-01 00:00:00.000000
$today_date = 2019-05-06 10:48:01
But I can't print the value of $comparison.
PHP Fatal error: Uncaught Error: Call to a member function diff() on string
How can I solve it?
You pass in $today_date to do_diifernce(), which is a string (as you have formatted it with format()). You can either pass $today in (which is an object), or include a condition that checks if its an object or not.
function do_difference($date_1, $date_2) {
// Check if the arguments were DateTime objects - if not, instantiate them as that
if (!($date_1 instanceof DateTime)) {
$date_1 = new DateTime($date_1);
}
if (!($date_2 instanceof DateTime)) {
$date_2 = new DateTime($date_2);
}
// Compare the difference and return the Y and m properties
$difference = $date_2->diff($date_1);
$difference->total = $difference->y . "." . $difference->m;
return $difference;
}
$today = new DateTime();
$comparison = do_difference($databse_date, $today);
You were playing date 2 as string which should be treated as datetime object to get the difference between two datetime objects.
function do_diifernce($date_1, $date_2)
{
$createDate1 = new DateTime($date_1);
$createDate2 = new DateTime($date_2);
$difference = $createDate2->diff($createDate1);
$sign = ($createDate1 < $createDate2 ? '-':'+');
$difference->total_difference = $difference->format("%r%a");
return $difference;
}
$databse_date = "2019-05-01 00:00:00";
$today_date = "2019-05-06 10:48:01";
$comparison = do_diifernce($databse_date, $today_date);
print_r($comparison);die;
Here is official doc.
You check that array as there is no difference of year and month as both dates belongs to same month and year so its coming 0.0
You got an error here: $difference = $date_2->diff($createDate, true);. AFAIK, the diff() function is deprecated after PHP 5.3.
If you want to calculate the difference between two dates, you can use date_diff as follows.
<?php
$date1 = date_create("2000-04-01");
$date2 = date_create("2019-04-06");
$diff = date_diff($date1, $date2);
?>
It throws an error, because you call format on date2, which returns a string, no DateTime object.
Remove the call to format, then your comparison should work.
All you have to do, is to replace the last line with the following:
$comparison=do_diifernce($databse_date, $today);

PHP DateInterval

I have this function witch return an array of date. I need to jump on every seven days from now until last year.
$date[] = $lastDate = (new \DateTIme('NOW'))->format('Y-m-d');
for ($i = 1; $i < 54; ++$i) { // 54 -> number of weeks in a year
$date[] = $lastDate = date('Y-m-d', strtotime('-7 day', strtotime($lastDate)));
}
return array_reverse($date);
It works but I can do better.
I would like to change it because using 54 for the number of weeks in a year is not very good. (it can change)
So I want to use the DateInterval php class.
I can have the date of the last year with :
$lastYear = date('Y-m-d', strtotime('-1 year', strtotime($lastDate)));
But I don't know how I can have my array with all my dates with the DateInterval class.
Can someone help me? I'm very bad with date manipulation :( ...
Here is an example array about what I need:
["2015-07-06", "2015-07-13", "2015-07-20", "2015-07-27", "2015-08-03", "2015-08-10", "2015-08-17", "2015-08-24", "2015-08-31", "2015-09-07", "2015-09-14", "2015-09-21", "2015-09-28", "2015-10-05", "2015-10-12", "2015-10-19", "2015-10-26", "2015-11-02", "2015-11-09", "2015-11-16", "2015-11-23", "2015-11-30", "2015-12-07", "2015-12-14", "2015-12-21", "2015-12-28", "2016-01-04", "2016-01-11", "2016-01-18", "2016-01-25", "2016-02-01", "2016-02-08", "2016-02-15", "2016-02-22", "2016-02-29", "2016-03-07", "2016-03-14", "2016-03-21", "2016-03-28", "2016-04-04", "2016-04-11", "2016-04-18", "2016-04-25", "2016-05-02", "2016-05-09", "2016-05-16", "2016-05-23", "2016-05-30", "2016-06-06", "2016-06-13", "2016-06-20", "2016-06-27", "2016-07-04"]
PHP got it 's own native DateInterval object. Here 's a short example how to use it.
$oPeriodStart = new DateTime();
$oPeriodEnd = new DateTime('+12 months');
$oPeriod = new DatePeriod(
$oPeriodStart,
DateInterval::createFromDateString('7 days'),
$oPeriodEnd
);
foreach ($oPeriod as $oInterval) {
var_dump($oInterval->format('Y-m-d));
}
So what we 've done here? For a period of dates you need a start date, an end date and the interval. Just test it for yourself. Have fun.
Try this:
$timestamp = strtotime("last Sunday");
$sundays = array();
$last_year_timestamp = strtotime("-1 year ",$timestamp);
while($timestamp >= $last_year_timestamp) {
if (date("w", $timestamp) == 0) {
$sundays[] = date("Y-m-d", $timestamp);
$timestamp -= 86400*7;
continue;
}
$timestamp -= 86400;
}

active recored where statement

In codeigniter I am trying to produce the following SQL statement:
SELECT *
FROM (`ea_users`,`ea_appointments`,`ea_services`)
WHERE `ea_appointments`.`id_users_customer` = `ea_users`.`id`
AND `ea_appointments`.`id_services` = `ea_services`.`id`
AND `ea_appointments`.`start_datetime`> '2015-07-18 00:00:00'
AND `ea_appointments`.`start_datetime`< '2015-07-18 23:59:59'
In the Active Record format I have tried this:
$day_start = date('Y-m-d 00:00:00', $day);
$day_end = date('Y-m-d 23:59:59', $day);
return $this->db->select('*')
->from('ea_appointments,ea_services,ea_users')
->where('ea_appointments.id_users_customer','ea_users.id')
->where('ea_appointments.id_services','ea_services.id')
->where('ea_appointments.start_datetime>',$day_start)
->where('ea_appointments.start_datetime<',$day_end)
->get()->result();
But it produces this instead:
SELECT *
FROM (`ea_appointments`, `ea_services`, `ea_users`)
WHERE `ea_appointments`.`id_users_customer` = 'ea_users.id'
AND `ea_appointments`.`id_services` = 'ea_services.id'
AND `ea_appointments`.`start_datetime>` '2015-07-18 00:00:00'
AND `ea_appointments`.`start_datetime<` '2015-07-18 23:59:59'
How do I get 'ea_users.id' and 'ea_services.id' to be translated as 'ea_users'.'id' and 'ea_services'.'id'? I have tried:
->where('ea_appointments.id_users_customer','ea_users'.'id')
->where('ea_appointments.id_services','ea_services'.'id')
But that produces this:
WHERE `ea_appointments`.`id_users_customer` = 'ea_usersid'
AND `ea_appointments`.`id_services` = 'ea_servicesid'
What is the correct format?
This should work:
(A space before the > < signs)
$day_start = date('Y-m-d 00:00:00', $day);
$day_end = date('Y-m-d 23:59:59', $day);
return $this->db->select('*')
->from('ea_appointments,ea_services,ea_users')
->where('ea_appointments.id_users_customer','ea_users.id')
->where('ea_appointments.id_services','ea_services.id')
->where('ea_appointments.start_datetime >',$day_start)
->where('ea_appointments.start_datetime <',$day_end)
->get()->result();
Assuming your original sql function works as you intended it before you tried to recreate it in codeigniter, You can simply do this.
$day_start = date('Y-m-d 00:00:00', $day);
$day_end = date('Y-m-d 23:59:59', $day);
$query = $this->db->query(SELECT *
FROM (ea_users, ea_appointments, ea_services)
WHERE ea_appointments.id_users_customer = ea_users.id
AND ea_appointments.id_services = ea_services.id
AND ea_appointments.start_datetime > {$day_start}
AND ea_appointments.start_datetime < {$day_end});
$query->result();
This worked:
$day_start = date('Y-m-d 00:00:00', $day);
$day_end = date('Y-m-d 23:59:59', $day);
return $this->db->select('ea_appointments.id,
ea_appointments.start_datetime,
ea_appointments.end_datetime,
ea_users.email,
ea_services.name')
->from('ea_appointments')
->join('ea_users', 'ea_appointments.id_users_customer = ea_users.id','left')
->join('ea_services', 'ea_appointments.id_services = ea_services.id','left')
->where('ea_appointments.start_datetime >',$day_start)
->where('ea_appointments.start_datetime <',$day_end)
->get()->result();
Join statements did the trick.

doctrine2 with php Datetime month

I have some events stored in a database using doctrine and Symfony2, and those events have a two datetime parameters, start and end of the event. I need to know if those events are active during a certain month, but i cant find a way. I tried with :
$now = new \Datetime('now');
$qb = $this->createQueryBuilder('e');
$qb->where('e.start <= :now AND e.end >= :now')
->setParameter('now', $now);
return $qb->getQuery()
->getArrayResult();
but it limits to the day, and not to the month. Is there a way to check it ? Thanks a lot !
You can use the following code
$monthStart = new DateTime(date('Y-m-01') . " 00:00:00");
$monthEnd = new DateTime(date('Y-m-t'). " 23:59:59");
$qb = $this->createQueryBuilder('e');
$qb
->where('e.start <= :end AND e.end >= :start')
->setParameter('start', $monthStart)
->setParameter('end', $monthEnd);
return $qb->getQuery()
->getArrayResult();

Date Query with Doctrine

I have fields in my table for date, but they contain everything - day, year and month. Can I write a query to get only the records, which has month equal to the current month? I can do this:
$today = new \DateTime();
$month = $today->format('m');
$cat = $em->getRepository('EMBudgetTrackerBundle:Expense')->find(1);
$ex_date = $cat->getDate();
and compare $month and $ex_date, but can I write some kind of query? Something like this:
public function getExpensesByMonth($month)
{
$q = $this->createQueryBuilder('e');
$q->select('e')
->where('e.date = :date')
->setParameter('date', $month);
return $q->getQuery()->getResult();
}
Thank you in advance! :)
If you database column is in DateTime format you can use the DateTime object in your query. As far as I know you can only query for time ranges though.
public function getExpensesByMonth($beginning, $end)
{
$q = $this->createQueryBuilder('e');
$q->select('e')
->where('e.date > :beginning')
->andWhere('e.date < :end')
->setParameter('beginning', $beginning)
->setParameter('end', $end);
return $q->getQuery()->getResult();
}

Categories